Cocos Creator v1.7.0 内测版发布帖(11月17日更新 rc.2)

经过验证确实返回的data的size为0了,代码如下:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
console.log("-----xhr.readyState " +xhr.readyState);
console.log("-----xhr.status " +xhr.status);
if (xhr.readyState === 4 ) {
if(xhr.status === 200){
console.log(’-----response 200-----’);
xhr.responseType = ‘arraybuffer’;
console.log(’-------xhr.response length’,xhr.response.byteLength);
xhr.response && (xhr.response.byteLength>0)&& saveFile(xhr.response);
}
}
}.bind(this);
xhr.open(“GET”, url, true);
xhr.timeout = 3000;
xhr.send();
1.7之前的版本返回的data的size不为0呢?

url给我一个,我试试。

大大辛苦了.了解了.
还有个小问题.那比如同样这个sprite,要给他换别的shader.是可以直接

this._program = new cc.GLProgram();
this._program.initWithString(_default_vert_no_mvp, this._frag_glsl);

这种方式,还是说当前 _this.program 直接有方法可以直接读取新的顶点着色器和片段着色器

麻烦提交一个 demo

换shader,你就new完cc.GLProgram之后,重新创建一个GLProgramState, 然后给sgnode设置替换一下。

url发了消息给你

谢谢反馈

给dumganhar发送了消息

恩 ,看到源码这一块了.辛苦了.

1.7 beta2外部拖拽文件到目录下 好像拖不进来

你的测试代码,在web上可以正常跑么?

web上可以的@dumganhar

xhr.responseType = ‘arraybuffer’;这行要在xhr.send()之前调用。不要在onreadstatechange status=200中调用。都成功了,再去设置responseType有何用。

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType

When setting responseType to a particular value, the author should make sure that the server is actually sending a response compatible to that format. If the server returns data that is not compatible to the responseType that was set, the value of response will be null.

1赞

改了之后可以用啦,1.7更严格啦?多谢

看上面修改后的回复。

responseType直接影响服务器返回的数据格式。

为什么之前的版本,这段代码可以通过呢?

代码重构了。代码逻辑有些区别。

好的,现在更清晰,多谢@dumganhar

1.6的代码取巧了。不管是text还是arraybuffer,都多加了一个\0字节:

    _dataSize = static_cast<uint32_t>(buffer->size());
    CC_SAFE_FREE(_data);
    _data = (char*) malloc(_dataSize + 1);
    _data[_dataSize] = '\0';
    memcpy((void*)_data, (const void*)buffer->data(), _dataSize);

这样在获取response的时候,就算返回的是text,在onreadystatechange 200后去设置responseType,然后调用response,会强制转换为arraybuffer。但这样做是不对的。1.7代码重构后,更加清晰。你可以看jsb_xhmlhttprequest.cpp中XMLHttpRequest中的代码,_responseText和_responseData是分开的,类型分别为std::string和cocos2d::Data;

因为responseType需要在send前就提前设置好的,而不是请求都成功了,临时修改,违背了本意。

1赞