首页 > nodejs返回arraybuffer给客户端的问题

nodejs返回arraybuffer给客户端的问题

我想通过node返回arraybuffer给客户端,但是遇到了问题:

server.js:

res.write(new Buffer([0,1,2]));
        res.writeHead(200,{'Access-Control-Allow-Origin':'*','Access-Control-Allow-Method':'GET,POST','Content-Type':'application/octet-stream'});
        res.end();

客户端代码:

var xhr = new XMLHttpRequest();
        xhr.responseType = "arraybuffer";
        xhr.open("post","http://localhost:8008/");
        xhr.onload = function(data){
            if(xhr.status === 200){
                console.log('succuess');
                var blo = new Blob(this.response);
                var reader = new FileReader();
                reader.readAsBinaryString(blo);
                reader.onload = function(f){
                    console.log(this.result);
                }
            }
        }

但是chrome控制台并没有打印结果,而是输出错误:

POST http://localhost:8008/ net::ERR_INVALID_CHUNKED_ENCODING

请问这是怎么回事呢,谢谢aaa


JS 的 ArrayBuffer 兼容性很差。
你改写成 xhr.responseType = "blob"; 试一下。
这个不敢保证有效。


wwwlwjefjasesfljawlfjlakwfjhkawezjsfiljafvlkijsdilkjaslfjcnkawfjciawfcoiajwoifvhnhaweonhf

【热门文章】
【热门文章】