简体   繁体   English

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

[英]XMLHttpRequest returning status 0 when sending Json object

I'm trying to get a response from the server but i'm getting a null status. 我正在尝试从服务器获取响应,但状态为空。 Where i'm getting wrong? 我哪里错了? I would like to send from an Script to a server within Max4live Patch. 我想从脚本发送到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;

i'm getting on the line : log("Status: "+xhr.status+ " "+xhr.statusText) : Status:0 null ... where I would like to get a json object from the server. 我上线:log(“ Status:” + xhr.status +“” + xhr.statusText): Status:0 null ...我想从服务器获取json对象。 Any idea? 任何想法? Thanks! 谢谢!

You make an asynchronous request (see third parameter of xhr.open() ), and your request is not finished when checking the status. 您发出一个异步请求(请参阅xhr.open()第三个参数),并且在检查状态时请求没有完成。

For async requests you need to implement an onreadystagechange handler. 对于异步请求,您需要实现一个onreadystagechange处理程序。

For example take a look at http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp 例如,看看http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM