iOS 和android XMLHttpRequest 没有正常回调(1.7.0和1.7.2测试都有同样的问题)

iOS 和android XMLHttpRequest 服务器异常情况没有执行onreadystatechange方法

我的代码

var xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = 1e4;
var strIndex = 0;
var str = “?”;
for (var k in data) {
“?” != str && (str += “&”);
str += k + “=” + data[k];
strIndex++;
}
0 === strIndex && (str = “”);
null == extraUrl && (extraUrl = HTTP.url);
var requestURL = extraUrl + path + encodeURI(str);
http://10.0.37.41:20000/get_user_room_data” == extraUrl + path && console.log(“端口错误”);
console.log(“sendRequest RequestURL:” + requestURL + " timeout="+xhr.timeout);
xhr.open(“GET”, requestURL, true);
console.log(“sendRequest open:” + requestURL + " timeout="+xhr.timeout);
cc.sys.isNative && xhr.setRequestHeader(“Accept-Encoding”, “gzip,deflate”, “text/html;charset=UTF-8”);
xhr.onreadystatechange = function() {
cc.log(“sendRequest xhr.readyState==” + xhr.readyState + " xhr.status==" + xhr.status + “xhr.responseText==” + xhr.responseText);

      };
      cc.vv && cc.vv.wc;
      console.log("sendRequest send:" + requestURL + " timeout="+xhr.timeout);
      xhr.send();
      console.log("sendRequest send end:" + requestURL + " timeout="+xhr.timeout);

xocde里面错误提示(android也有这个问题):
JS: sendRequest RequestURL:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
JS: sendRequest open:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
JS: sendRequest send:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
JS: sendRequest send end:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
2017-12-06 14:10:52.226799+0800 MJClient-mobile[64887:2922595] Starting to load http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1
2017-12-06 14:10:52.233631+0800 MJClient-mobile[64887:2922310] TIC TCP Conn Failed [3:0x60800017be40]: 1:61 Err(61)
2017-12-06 14:10:52.234340+0800 MJClient-mobile[64887:2922310] Task <684D25E4-D4D7-4880-9804-69CE2DD0B965>.<0> HTTP load failed (error code: -1004 [1:61])
2017-12-06 14:10:52.234607+0800 MJClient-mobile[64887:2921881] NSURLConnection finished with error - code -1004
Response failed, error buffer: Could not connect to the server.
XMLHttpRequest_finalize, 0x7fd7b2035530 …
XMLHttpRequest_finalize, 0x7fd7b20374e0 …

xhr的bug 很久很久以前就有了 1.5.x 的时候 就有 一直摒弃这个接口 用downloader 但是 1.6.1之后的downloader有问题 真是头疼啊

代码就不能格式化一下么?怎么看
用markdown语法。
放在

你的代码

里面。

        ```
        var xhr = cc.loader.getXMLHttpRequest();
        cc.log(xhr);
        xhr.timeout = 10000;
        cc.log(xhr.timeout);
        var strIndex = 0;
        var str = "?";

        for (var k in data) {
            if (str != "?") {
                str += "&";
            }
            str += k + "=" + data[k];
            strIndex++;
        }

        if (strIndex === 0) {
            str = "";
        }

        if (extraUrl == null) {
            extraUrl = HTTP.url;
        }

        var requestURL = extraUrl + path + encodeURI(str);
        //初始化HTTP请求参数,true表示异步,通常需要一个 onreadystatechange 事件句柄。
        console.log("sendRequest RequestURL:" + requestURL + " timeout="+xhr.timeout);
        xhr.open("GET", requestURL, true);
        console.log("sendRequest open:" + requestURL + " timeout="+xhr.timeout);
        if (cc.sys.isNative) {
            //向一个打开但未发送的请求设置或添加一个 HTTP 请求。
            xhr.setRequestHeader("Accept-Encoding", "gzip,deflate", "text/html;charset=UTF-8");
        }


        xhr.onreadystatechange = function() {
            cc.log("xhr.readyState==" + xhr.readyState + "  xhr.status==" + xhr.status + "xhr.responseText==" + xhr.responseText);
        };

        if (cc.vv && cc.vv.wc) {}
        
        console.log("sendRequest send:" + requestURL + " timeout="+xhr.timeout);
        xhr.send();
        console.log("sendRequest send end:" + requestURL + " timeout="+xhr.timeout);
        
        ```
        
        xocde里面错误提示(android也有这个问题):
        JS: sendRequest RequestURL:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
        JS: sendRequest open:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
        JS: sendRequest send:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
        JS: sendRequest send end:http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1 timeout=10000
        2017-12-06 14:10:52.226799+0800 MJClient-mobile[64887:2922595] Starting to load http://10.0.37.41:20000/login?type=1&source=0&deviceId=1512539212884&deviceType=2&gameClass=1&version=1.0.0&build=1
        2017-12-06 14:10:52.233631+0800 MJClient-mobile[64887:2922310] TIC TCP Conn Failed [3:0x60800017be40]: 1:61 Err(61)
        2017-12-06 14:10:52.234340+0800 MJClient-mobile[64887:2922310] Task <684D25E4-D4D7-4880-9804-69CE2DD0B965>.<0> HTTP load failed (error code: -1004 [1:61])
        2017-12-06 14:10:52.234607+0800 MJClient-mobile[64887:2921881] NSURLConnection finished with error - code -1004
        Response failed, error buffer: Could not connect to the server.
        XMLHttpRequest_finalize, 0x7fd7b2035530 ... 
        XMLHttpRequest_finalize, 0x7fd7b20374e0 ...

一开始请求另外一个链接,能正常返回(错误状态),马上再请求下个链接就一直没有执行onreadystatechange

嘿,异常是要监听 xhr.onerror的
还有,超时要监听 xhr.ontimeout
这两个是必须要设置的。你都没有。。。

1赞

我看html5 ,这两个函数,没有,我以为没有…

大佬你好,
请教:xhr,web上返回status==0,模拟器正常
:grin:

不管是成功还是任何原因失败,onreadystatechange都会被调用啊。

https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
原版的xhr没有这两个接口啊,应该是native平台才加上的吧。新加的接口可以在cocos的文档里面注明一下吗,开发者还是很容易被坑的。