我正在尝试从服务器获取响应,但我得到的是空状态。我哪里出错了? 我想从 Max4live Patch 中的脚本发送到服务器。
var xhr = new XMLHttpRequest();
var url = 'myURL'
log("1");
xhr.open('PUT', url, true, 'myUser', 'myPass');
log("2");
xhr.setRequestHeader('Content-Type', 'application/json');
log("4");
xhr.send(JSON.stringify(JsonExport));
log("5");
log("Status: "+xhr.status+ " "+xhr.statusText);
if (xhr.status == 4) {
var userInfo = JSON.parse(xhr.responseText);
log("Status");
log(xhr.responseText);
log(userInfo);
} else {
log("Response");
log(xhr.responseText);
}
//log(xhr.responseType["json"]);
delete xhr;
我正在上线:log("Status: "+xhr.status+ ""+xhr.statusText) : Status:0 null...我想从服务器获取 json 对象。任何想法? 谢谢!
最佳答案
您发出异步请求(参见xhr.open()
的第三个参数),检查状态时您的请求尚未完成。
对于异步请求,您需要实现一个 onreadystagechange
处理程序。
例如,看看 http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
关于javascript - XMLHttpRequest 发送 Json 对象时返回状态 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914878/