Javascript 闭包js框架-将ArrayBuffer转换为字符串
我正在开发一个使用()的javascript应用程序 我收到一个带有403响应的ajax响应,我需要解析响应体以确定细节 responseType设置为arraybuffer-因此我希望能够将响应转换为字符串以读取其内容:Javascript 闭包js框架-将ArrayBuffer转换为字符串,javascript,ajax,google-closure,Javascript,Ajax,Google Closure,我正在开发一个使用()的javascript应用程序 我收到一个带有403响应的ajax响应,我需要解析响应体以确定细节 responseType设置为arraybuffer-因此我希望能够将响应转换为字符串以读取其内容: if (this.xhr_.responseType == 'arraybuffer') { var ab = new Uint8Array(this.xhr_.response); console.log(this.xhr_.response); c
if (this.xhr_.responseType == 'arraybuffer')
{
var ab = new Uint8Array(this.xhr_.response);
console.log(this.xhr_.response);
console.log(ab);
}
使用closure框架构建时,我得到以下错误:
./build/../build/../lib/util/ajax_request.js:441: ERROR - actual parameter 1 of Uint8Array does not match formal parameter
found : *
required: (Array.<number>|ArrayBuffer|ArrayBufferView|null|number)
var ab = new Uint8Array(this.xhr_.response);
/build/。/build/。/lib/util/ajax\u request.js:441:ERROR-Uint8Array的实际参数1与形式参数不匹配
发现:*
必需:(数组。| ArrayBuffer | ArrayBufferView | null |编号)
var ab=新的Uint8Array(此.xhr_xr.response);
因此,我发现不可能将响应传递到Uint8Array构造函数。是否有方法强制转换响应以保持闭包安静?如果responsetype为arraybuffer,则需要通过以下方式循环:
if (this.xhr_.responseType == 'arraybuffer')
{
var ab = new Uint8Array(this.xhr_.response);
for (var i = 0, buffer = ''; i < ab.length; i++)
{
buffer += String.fromCharCode(payload[i]);
}
}
if(this.xhr..responseType=='arraybuffer')
{
var ab=新的Uint8Array(此.xhr_xr.response);
对于(变量i=0,缓冲区=“”;i
希望这会对您有所帮助。我找到了一个有效的解决方案-如何在闭包框架中强制转换-我希望这对其他人有所帮助
if (this.xhr_.responseType == 'arraybuffer')
{
var response = /** @type {ArrayBuffer} */ (this.xhr_.response);
var sBuffer = String.fromCharCode.apply(null, new Uint8Array(response));
console.log('response ArrayBuffer to string: ' + sBuffer);
}
谢谢,但我的问题是var ab=new-Uint8Array(这个.xhr\响应);失败。不依赖于关闭框架。如果您需要将ArrayBuffer转换为字符串,这对vanilla非常有效。我猜-但相关部分是/**@type{ArrayBuffer}*/实际上具有闭包框架中强制转换的效果-如果没有此功能,构建将失败。