ajax 中獲取和發(fā)送二進(jìn)制數(shù)據(jù)的方法
標(biāo)簽:
项目中用到二进制数据 ,一般的ajax请求并不能满足需求,所以看了下XMLHttpRequest对象,用xhr.response来获得二进制数据,而不是responseText,示例如下:
var xhr = new XMLHttpRequest();
xhr.open('GET',url, true);
xhr.responseType = 'blob'; // 二进制大对象
xhr.onload = function(e) {
if (this.status == 200) {
// get binary data as a response
var blob = this.response;
}
};
xhr.send();
// 真正方法
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
// response is unsigned 8 bit integer
var responseArray = new Uint8Array(this.response);
// 下面对二进制数据的处理
};
xhr.send();
點(diǎn)擊查看更多內(nèi)容
10人點(diǎn)贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦