发送Json对象时,XMLHttpRequest返回状态0

XMLHttpRequest returning status 0 when sending Json object

本文关键字:返回 状态 XMLHttpRequest Json 对象 发送      更新时间:2023-09-26

我正试图从服务器获得响应,但得到的状态为null。我哪里错了?我想从脚本发送到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("状态:"+xhr.Status+"+xher.statusText):状态:0 null。。。在那里我想从服务器获得一个json对象。知道吗?谢谢

您发出了一个异步请求(请参阅xhr.open()的第三个参数),在检查状态时您的请求尚未完成。

对于异步请求,您需要实现一个onreadystagechange处理程序。

例如,看看http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp