跨域XMLHtt prequest不工作​​在Chrome封装应用尽管拥有权限的网址 [英] Cross-Origin XMLHttpRequest not working in Chrome Packaged App despite having url in permissions

查看:263
本文介绍了跨域XMLHtt prequest不工作​​在Chrome封装应用尽管拥有权限的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的问题对SO,<一本其它问题href="http://stackoverflow.com/questions/11375729/xmlhtt$p$pquest-not-working-in-google-chrome-packaged-web-app">"XMLHtt$p$pquest不工作的谷歌浏览器打包的Web应用程序。但是,所选择的答案是不是为我工作。我有我试图请求列入清单的权限的URL。我有一种感觉,这有做跨站点脚本prevention,正如我在跨域XMLHtt $阅读p $ pquest 嵌入内容文档的浏览器。

I'm having a similar issue to this other question on SO, "XMLHttpRequest not working in Google Chrome Packaged Web App". However, the selected answer is not working for me. I do have the url that I'm trying to request listed in the manifest permissions. I have a feeling this has to do with cross-site-scripting prevention, as I read about in the Cross-Origin XMLHttpRequest and Embed Content docs for Chrome.

当我检查响应,一切都空了,状态没有 code,没有响应,没有状态文本,就像试图让跨域请求在XSS时,在此等问题看到发生了什么,:的从XMLHtt prequest 空responseText的。

When I inspect the response, everything's empty, no status code, no response, no statusText, just like what happens when trying to make a request across domains as in XSS, as seen in this SO question: Empty responseText from XMLHttpRequest.

下面是我的清单:

{
    "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" }
}

我使用这个功能做请求:

I'm using this function to do the requests:

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);
}

网址我想要求是相当基本的一些上涨了最终的查询字符串参数,看起来是这样的:

The url I'm trying to request is fairly basic with some query string parameters tacked on the end and looks like this:

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

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

Which, when loaded in the browser, returns simple and valid JSON:

{
    "ret" : [

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

我使用来测试开发者控制台的功能是这样的:

The function I'm using to test in the developer console is this:

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

所有这一切都打印到控制台错误: XHR 在那一刻是:

XMLHtt prequest {状态文本:,状态:0,回应:,responseType:,responseXML的:空...}

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

One other thing which may need to be considered is that this url I'm requesting is behind authentication on my server. I'm logged in and am able to access the page, regardless of which tab I'm using, so I don't think that's the problem, but it still might be.

推荐答案

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

Apps don't share your browser's cookie store. They're like native apps in that respect. So your last paragraph is likely the issue. Change to a server that doesn't require authentication as a test to confirm it. Then investigate chrome.identity.

这篇关于跨域XMLHtt prequest不工作​​在Chrome封装应用尽管拥有权限的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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