gpt4 book ai didi

javascript - 尽管权限中有 url,但跨源 XMLHttpRequest 无法在 Chrome 打包应用程序中工作

转载 作者:行者123 更新时间:2023-11-28 01:55:24 25 4
gpt4 key购买 nike

我遇到了与 SO 上的其他问题类似的问题,"XMLHttpRequest not working in Google Chrome Packaged Web App" 。但是,所选答案对我不起作用。我确实在 list 权限中列出了我尝试请求的网址。我有一种感觉,这与跨站点脚本预防有关,正如我在 Cross-Origin XMLHttpRequest 中读到的那样。和 Embed Content Chrome 文档。

当我检查响应时,一切都是空的,没有 status 代码,没有 response,没有 statusText,就像尝试时发生的情况一样如 XSS 中那样跨域发出请求,如以下 SO 问题所示:Empty responseText from XMLHttpRequest

这是我的 list :

{
"manifest_version" : 2,
"name": "{name}",
"description" : "{description}",
"version" : "0.1",
"minimum_chrome_version": "23",

"permissions": [
"idle",
"storage",
"notifications",
"https://prefix.domain.suffix/*",
"https://prefix.domain.suffix/sub/"
],

"app": {
"background": {
"scripts": ["models.js", "background.js"]
}
},


"icons": { "16": "icon-16.png", "128": "icon-128.png" }
}

我正在使用此函数来执行请求:

function xhr(url, callback){
var timeout= 2000;
var xhr = new window.XMLHttpRequest();
xhr.ontimeout = function () {
console.error("The request for " + url + " timed out.");
};
xhr.onerror = function(){
console.error("error: "+xhr.statusText);
}
xhr.onload = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
window.console.log("response: "+xhr.response);
callback(JSON.parse(xhr.response));
} else {
console.error(xhr.statusText);
}
}
};
xhr.open("GET", url, true);
xhr.timeout = timeout;
xhr.send(null);
}

我尝试请求的网址相当基本,末尾附加了一些查询字符串参数,如下所示:

https://prefix.domain.suffix/sub/serve.aspx?param1=val1&param2=val2

当在浏览器中加载时,返回简单且有效的 JSON:

{
"ret" : [

{ "date": 1380603600000, "foo": bar1 },
{ "date": 1380776400000, "foo": bar2 }
]
}

我用来在开发者控制台中测试的函数是这样的:

xhr('https://prefix.domain.suffix/sub/serve.aspx?param1=val1&param2=val2', function(e){ console.log(e); });

打印到控制台的所有内容是 error:,此时的 xhr 是:

XMLHttpRequest {statusText:“”,状态:0,响应:“”,responseType:“”,responseXML:null…}

可能需要考虑的另一件事是我请求的这个网址落后于我的服务器上的身份验证。我已登录并能够访问该页面,无论我使用哪个选项卡,所以我认为这不是问题,但仍然可能是问题。

最佳答案

应用程序不会共享您浏览器的 Cookie 存储。在这方面它们就像 native 应用程序。所以你的最后一段可能是问题所在。更改为不需要身份验证的服务器作为测试来确认。然后调查 chrome.identity。

关于javascript - 尽管权限中有 url,但跨源 XMLHttpRequest 无法在 Chrome 打包应用程序中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19190505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com