XMLHttpRequest 在发送 Json 对象时返回状态 0
XMLHttpRequest returning status 0 when sending Json object
我正在尝试从服务器获得响应,但我得到的是空状态。我哪里错了?
我想在 Max4live 补丁中从脚本发送到服务器。
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
我正在尝试从服务器获得响应,但我得到的是空状态。我哪里错了? 我想在 Max4live 补丁中从脚本发送到服务器。
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