XML HTTP 请求乔德尔
XML HTTP request Jodel
我花了很多时间试图自己解决这个问题,但找不到解决方案。
这是我的 jodelclient 对象中的一个函数。
this.sendPost = function(JodelPost)
{
var httpRequest = new XMLHttpRequest(); //create new httpRequest
httpRequest.onreadystatechange = function (data) //This is the function that gets called when you recieve the server response
{
console.log(data); //Prints out the server response
}
var url = 'https://api.go-tellm.com/api/v2/posts/?'; //BaseUrl for jodel api
httpRequest.open('POST', url+JSON.stringify(JodelPost));
httpRequest.setRequestHeader('Authorization', 'Bearer ' + this.token); //For a valid authirazation
httpRequest.send(); //send it flying!
};
服务器的响应是
"POST https://api.go-tellm.com/api/v2/posts/? 400 (Bad Request)"
和
"Missing required property: location"
这是对 JodelPost 变量进行字符串化的结果示例。
{
"color": "FFBA00",
"location": {
"city": "Uppsala",
"country": "46",
"loc_accuracy": 0,
"loc_coordinates": {
"lat": "68.805532",
"lng": "2.943903"
},
"name": "Uppsala"
},
"message": "HelloWorld"
}
当我试图自己解决这个问题时,我发现这个 github 存储库在做同样的事情,但在 python 中。里面可能会有一些有用的信息。
提前致谢!我对此很陌生,所以感谢简单的解释。
在我看来请求没有正确创建。您正在尝试使用 URL 发送数据,但它应该在 XHR object 的 send
函数中传递。
此外,每次状态更改时都会调用 onreadystatechange
函数,但您很可能希望在状态达到 4
且状态为 200
时检查数据.如需更多阅读,请参阅 here。
您还需要为您的请求设置 content-type header。
尝试这样的事情:
this.sendPost = function(JodelPost)
{
var httpRequest = new XMLHttpRequest(); //create new httpRequest
httpRequest.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
console.log(httpRequest.responseText);
}
}
var url = 'https://api.go-tellm.com/api/v2/posts/?'; //BaseUrl for jodel api
httpRequest.open('POST', url);
httpRequest.setRequestHeader('Authorization', 'Bearer ' + this.token);
httpRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
httpRequest.send(JSON.stringify(JodelPost));
};
我花了很多时间试图自己解决这个问题,但找不到解决方案。
这是我的 jodelclient 对象中的一个函数。
this.sendPost = function(JodelPost)
{
var httpRequest = new XMLHttpRequest(); //create new httpRequest
httpRequest.onreadystatechange = function (data) //This is the function that gets called when you recieve the server response
{
console.log(data); //Prints out the server response
}
var url = 'https://api.go-tellm.com/api/v2/posts/?'; //BaseUrl for jodel api
httpRequest.open('POST', url+JSON.stringify(JodelPost));
httpRequest.setRequestHeader('Authorization', 'Bearer ' + this.token); //For a valid authirazation
httpRequest.send(); //send it flying!
};
服务器的响应是
"POST https://api.go-tellm.com/api/v2/posts/? 400 (Bad Request)"
和
"Missing required property: location"
这是对 JodelPost 变量进行字符串化的结果示例。
{
"color": "FFBA00",
"location": {
"city": "Uppsala",
"country": "46",
"loc_accuracy": 0,
"loc_coordinates": {
"lat": "68.805532",
"lng": "2.943903"
},
"name": "Uppsala"
},
"message": "HelloWorld"
}
当我试图自己解决这个问题时,我发现这个 github 存储库在做同样的事情,但在 python 中。里面可能会有一些有用的信息。
提前致谢!我对此很陌生,所以感谢简单的解释。
在我看来请求没有正确创建。您正在尝试使用 URL 发送数据,但它应该在 XHR object 的 send
函数中传递。
此外,每次状态更改时都会调用 onreadystatechange
函数,但您很可能希望在状态达到 4
且状态为 200
时检查数据.如需更多阅读,请参阅 here。
您还需要为您的请求设置 content-type header。
尝试这样的事情:
this.sendPost = function(JodelPost)
{
var httpRequest = new XMLHttpRequest(); //create new httpRequest
httpRequest.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
console.log(httpRequest.responseText);
}
}
var url = 'https://api.go-tellm.com/api/v2/posts/?'; //BaseUrl for jodel api
httpRequest.open('POST', url);
httpRequest.setRequestHeader('Authorization', 'Bearer ' + this.token);
httpRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
httpRequest.send(JSON.stringify(JodelPost));
};