1.7.0 XMLHttpRequest读到的内容是空的,1.6.2正常
代码贴出来看看
代码 一
global.Utils.httpRequest(remote_url, "", function(xhr, filename){
cc.log("responseText xxxxxxxxxxxxxxxxxx", JSON.stringify(xhr), typeof xhr.responseText)
var configs = JSON.parse(new String(xhr.responseText) )
cc.log("xxxxxxxxxxxxxxxxxx-----------",xhr.responseText)
for (var index = 0; index < self.pv_ads.content.childrenCount; index++) {
var child = self.pv_ads.content.children[index]
var cfg = configs[index] || null
child.active = configs[index] != null;
if(cfg) {
self.total_page_count++
var script = child.getComponent("BannerItem")
script.init(configs[index])
}
}
self.pv_ads.scrollToPage(0, 0);
})
代码2
httpRequest : function(address, filename, callback){
var xhr = new XMLHttpRequest()
var self = this
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
if(callback) {
callback(xhr, filename)
}
}
}
xhr.responseType = "arraybuffer"
xhr.open("GET", address, true)
xhr.send()
},
上面调用的是下面的,1.6.2是正常的,1.7获取到的字符串长度有问题,是个空串
加上这两句看看打印啥
console.log("-----xhr.readyState----" +xhr.readyState);
console.log("-----xhr.status----" +xhr.status);
这加上这句看看
console.log(’-------xhr.response length’,xhr.response.byteLength);
2017-11-29T06:37:38.309Z - normal: Simulator: JS: -----xhr.readyState----4
2017-11-29T06:37:38.310Z - normal: Simulator: JS: -----xhr.status----200
2017-11-29T06:37:38.310Z - normal: Simulator: JS: -------xhr.response length 316
2017-11-29T06:37:38.310Z - normal: Simulator: JS: responseText xxxxxxxxxxxxxxxxxx {} string
2017-11-29T06:37:38.310Z - normal: Simulator: ERROR: SyntaxError: JSON Parse error: Unexpected EOF, location: assets/Script/GameHall/GameHall.js:90:37
返回的是json字符串的话,不用设置xhr.responseType = "arraybuffer"吧?
请求的是图片的话设置的这个,普通json字符串都没置这个。
我也用的1.7没有问题