Chrome扩展xhr跨域请求提供错误:“Access-Control-Allow-Origin不允许”。

问题描述:

我似乎无法从我的popup.html请求此网址:
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=dogs。

I cant seem to request this url: "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=dogs" from my popup.html.

我得到:

XMLHttpRequest无法加载 https://ajax.googleapis.com/ajax/services/search/web ? v = 1.0& q =狗。

XMLHttpRequest cannot load https://ajax.googleapis.com/ajax/services/search/web? v=1.0&q=dogs. Origin chrome-extension://nemobemncffjipfgpaffgiigbjhkpden is not allowed by Access-Control-Allow-Origin.

以下是我的清单:

{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"popup":"popup.html"
},
"permissions": [
"tabs","http://*/","https://*/"
]
}

和我的代码:

$.ajax({
            type: 'GET', //making a get request
            url:   "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=dogs",
            success: function (data) {
              document.write(data);
            }
            });

请帮助。

查看我的manifest.json中的权限列:

Look at the permission column in my manifest.json:

"permissions": ["tabs", "notifications", "http://*/*", "https://*/*"],

所以,你的url模式是错误的,应该是http:// / ,而不是http:// * /。

So, your url pattern is wrong, it should be "http:///", not "http://*/".