Chrome扩展程序:取消XMLHttpRequest(状态== 0) [英] Chrome Extension: XMLHttpRequest canceled (status == 0)

查看:285
本文介绍了Chrome扩展程序:取消XMLHttpRequest(状态== 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 XMLHttpRequest 从服务器加载json文件。
但我只得到 status == 0 ,而chrome dev控制台显示:(取消)



我可以用这个(简化)代码重现这一点:

  var httpReq = new window.XMLHttpRequest(); 
httpReq.onreadystatechange =函数(数据){
if(httpReq.readyState == 4){
alert(httpReq.status);
}
}
httpReq.open(GET,http://dl.dropbox.com/[...]/data.json);
httpReq.send(null);

此代码停止工作,因为它位于扩展上下文中。
同样的函数也用于从其他服务器(也是crossdomain)加载文件,没有任何问题。



以下是清单部分:

 permissions:[http://theOtherWorkingServer.com/*,http://dl.dropbox.com/* ,https://dl.dropbox.com/*],

我读过很多关于这个问题,但没有找到工作解决方案。
(主页面没有重装,也没有重定向)



任何建议??

解决方案

您收到的错误是一个重定向执行的标志,指向与您声明的任何权限不匹配的URL。

验证我的声明中,将 *:// * / * < all_urls> 权限添加到清单文件中,您会注意到请求已成功完成。



要确定遵循哪些重定向,请按照以下步骤操作:


  1. 打开开发人员工具,转到网络标签。

  2. 将网址粘贴到多功能框中,然后按Enter键

  3. 阅读开发人员工具中的网址。

dl.dropbox.com 实际上会重定向到 dl.dropboxusercontent.com 。此域必须添加到清单文件的权限部分,以解决问题:

 permissions:[
http://theOtherWorkingServer.com/*,
*://dl.dropbox.com/*,
*://dl.dropboxusercontent.com/*
],

(该方案中的通配符匹配 http https - 请参阅匹配模式


I try to load a json file from a server with XMLHttpRequest. But I only get status==0 and the chrome dev console says: "(canceled)"

I can reproduce this with this (simplified) code:

var httpReq = new window.XMLHttpRequest();
httpReq.onreadystatechange = function(data) { 
  if (httpReq.readyState == 4) {
    alert(httpReq.status);
  }                
}                    
httpReq.open("GET", "http://dl.dropbox.com/[...]/data.json");
httpReq.send(null);

This code stops working, as it is in the extension context. The same function is also used to load files from an other server (also crossdomain), without any problem.

Here is the manifest part:

"permissions": [ "http://theOtherWorkingServer.com/*", "http://dl.dropbox.com/*", "https://dl.dropbox.com/*" ],

I've read a lot about this problem, but found no working solution. (The main page is not reloading and also not redirected at this time)

Any suggestions??

解决方案

The error you're getting is a sign that a redirect was performed, to an URL which does not match any of your declared permissions.
To verify my statement, add the *://*/* or <all_urls> permission to the manifest file, and you will notice that the request completes successfully.

To determine which redirects are followed, follow the following steps:

  1. Open the developer tools, go to the Network tab.
  2. Paste the URL in the omnibox, and hit enter
  3. Read the URLs in the developer tools.

As you can see in the previous picture, dl.dropbox.com actually redirects to dl.dropboxusercontent.com. This domain has to be added to the permission section of your manifest file, to fix the problem:

"permissions": [
    "http://theOtherWorkingServer.com/*",
    "*://dl.dropbox.com/*",
    "*://dl.dropboxusercontent.com/*"
],

(the the wildcard at the scheme matches http and https - see match patterns)

这篇关于Chrome扩展程序:取消XMLHttpRequest(状态== 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆