java状态模式exten,Chrome扩展程序:取消XMLHttpRequest(状态== 0)

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 permission to the manifest file, and you will notice that the request completes successfully.

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

Open the developer tools, go to the Network tab.

Paste the URL in the omnibox, and hit enter

Read the URLs in the developer tools.

KSzjQ.png

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值