简体   繁体   English

用于responseType arraybuffer的xmlHttpRequest GET在电子中获得空响应

[英]xmlHttpRequest GET for responseType arraybuffer gets empty response in electron

I am using electron version 1.7.9. 我正在使用电子版1.7.9。 I need to download a zip file in my app. 我需要在我的应用中下载一个zip文件。 Following code is used for the same. 以下代码用于相同的代码。

  var urlFetch = <path to the zip file I want to download> var xhr = new XMLHttpRequest(); xhr.open('GET', urlFetch, true); xhr.responseType = "arraybuffer"; xhr.onloadend = function () { var status = xhr.status; if (status == 200) { if (undefined !== xhr.response) { successCallback(xhr.response); } else { failureCallback(); } } else { console.error("Failed to fetch " + urlFetch + ", status - " + status); failureCallback(); } }; xhr.send(); 

On Windows 10 as well as on MacOs HighSierra, this code always gets undefined xhr.response in spite of status being 200. 在Windows 10以及MacOs HighSierra上,尽管状态为200,但此代码始终会得到未定义的xhr.response。

xhr.responseText is populated with string, but what I need is the binary data of the zip file that I can use to store into a file for further use. xhr.responseText填充有字符串,但是我需要的是zip文件的二进制数据,可以将其存储到文件中以备将来使用。

I have also tried adding following, but with no success. 我也尝试添加以下内容,但没有成功。

 xhr.setRequestHeader("Content-Type", "application/zip"); 

Is there anything missing in the way I am coding or is this a bug in electron/chrome etc.? 我的编码方式中是否缺少任何东西,或者这是电子/铬等中的错误?

The same code works perfectly outside of electron in a stand alone HTML. 在独立的HTML中,相同的代码可以完美地在电子之外运行。

The problem was with the node js module xmlHttpRequest which was included. 问题出在其中包含了节点js模块xmlHttpRequest。 Things started working when I removed this module. 删除此模块后,一切开始正常工作。 I was not able to see the XHR requests in the "network" tab of dev tools in my electron app. 我在电子应用程序的开发工具的“网络”选项卡中看不到XHR请求。 After removing the module, I can now see the requests and things work as desired. 删除模块后,我现在可以看到请求,并且一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript XMLHttpRequest.responseType as &quot;arraybuffer&quot; -- 如何获取当前的数组缓冲区块 - JavaScript XMLHttpRequest.responseType as "arraybuffer" -- how to get current arraybuffer chunk new XMLHttpRequest()。responseType为空 - new XMLHttpRequest().responseType empty javafx webview javascript XMLHttpRequest responseType数组缓冲区 - javafx webview javascript XMLHttpRequest responseType arraybuffer 如何检测XMLHttpRequest是否支持responseType =“arraybuffer”? - How to feature detect if XMLHttpRequest supports responseType = “arraybuffer”? 如果responseType是arraybuffer,如何从$ http读取JSON错误响应 - How to read JSON error response from $http if responseType is arraybuffer 将HTTP responseType设置为arraybuffer - Set HTTP responseType as arraybuffer XMLHttpRequest获取请求响应为空,即使有回调 - XMLHttpRequest get request response is empty, even with callback XMLHttpRequest始终会得到一个空响应,而通过浏览器直接ping相同的URL会得到正确的响应 - XMLHttpRequest always gets an empty response, while directly pinging the same url via browser gets the right response XmlHttpRequest返回空响应 - XmlHttpRequest returning empty response XMLHttpRequest的空响应 - Empty response of XMLHttpRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM