[英]How to check whether my XMLHttpRequest post method success or not?
I am not very much used to with API's. 我不太习惯使用API。 I have written one function in which I am sending data to API.
我编写了一个向API发送数据的函数。 I have called this function on a textbox text change event.
我在文本框文本更改事件上调用了此函数。 I am not getting whether it is working or not because I am not getting any error.
我没有得到它是否正在工作,因为我没有得到任何错误。 Below is the function:
下面是功能:
function sendAPIData(url,introduction,coordinateid){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://192.168.51.212:3368/casigo/sDAGinput",true);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
var input = JSON.stringify({
"coordinate": coordinateid,
"url": url,
"introduction": introduction
});
xhttp.send(input);
}
It should return me some input string. 它应该返回我一些输入字符串。
To check the server's response a callback function can be used: 要检查服务器的响应,可以使用回调函数:
function sendAPIData(url,introduction,coordinateid) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://192.168.51.212:3368/casigo/sDAGinput",true);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
var input = JSON.stringify({
"coordinate": coordinateid,
"url": url,
"introduction": introduction
});
// callback function
xhttp.onload = function(res) {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
console.log("OK");
} else {
console.error("ERROR");
}
}
};
xhttp.send(input);
}
xhttp.onload
is a function that will be called once the XMLHttpRequest is completed. xhttp.onload
是XMLHttpRequest完成后将被调用的函数。 Its argument res
is the server's response. 它的参数
res
是服务器的响应。
XMLHttpRequest contains status
and readyState
properties which can be used by using the onreadystatechange
event. XMLHttpRequest包含
status
和readyState
属性,可以通过使用onreadystatechange
事件来使用它们。 Something like the below code should work. 类似于以下代码的东西应该可以工作。
xhttp.onreadystatechange=function(){
//readyState == 4 means the request is done and a response is received from the server
if (xhttp.readyState==4 && xhttp.status==200 && typeof(xhttp.responseText) == 'string'){
//Do something
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.