无法在Chrome应用中运行xmlhttprequest:临时标题&没有'Access-Control-Allow-Origin'

时间:2014-03-21 00:04:05

标签: google-chrome google-chrome-extension google-chrome-app

我正在构建一个Chrome应用程序,将Get HTTPRequest发送到外部API:
我得到了答案:

  

XMLHttpRequest无法加载   http://developer.echonest.com/api/v4/artist/profile?api_key=FILDTEOIK2HBORODV&name=weezer。   请求中不存在“Access-Control-Allow-Origin”标头   资源。 Origin'chrome-extension:// ihdfphmemcdeadpnjkhpihmcoeiklphe'   因此不允许访问。

  • 我确实在我的清单中允许外部域使用权限(以防止阻止跨域请求)
  • 当我在地址栏中输入网址时,它可以正常使用

似乎 Chrome阻止了我的请求,我甚至尝试直接在HTML页面中加载脚本并且它不起作用(相同的消息但是原点为“null”)(哦,它确实不允许我通过更改标题中的Origin来作弊 我还在控制台中得到了着名的“警告:临时标题”,这让我觉得Chrome阻止了我的请求,我查看了其他Stack Overflow问题,但除了运行chrome:// net-internals并寻找东西我没有关于我找不到任何好答案的第一个线索(我确实运行了chrome:// net-internals,但实际上没有任何意义)。

以下是请求:

function update_stations() {
    var xhr = new XMLHttpRequest();
    xhr.open("Get","http://developer.echonest.com/api/v4/artist/profile?api_key=FILDTEOIK2HBORODV&name=weezer", true);
    xhr.responseType = "json";
    xhr.onreadystatechange = function() {
        console.log("Essai");
        console.log(xhr.readyState);
        console.log(xhr);
        document.getElementById("resp").innerText = xhr;
    }
    xhr.send()
}

任何想法(会高度赞赏)?

1 个答案:

答案 0 :(得分:1)

Cross-Origin XMLHttpRequest chrome开发人员文档说明主机必须列在清单文件的权限中。

我从上面获取了XHR代码并将其包含在hello world示例中。将以下内容添加到manifest.json后,它可以正常工作。

  "permissions": [
    "http://*.echonest.com/"
  ]