Phonegap jQuery ajax 请求不起作用

发布于 2024-12-27 22:53:55 字数 1516 浏览 0 评论 0原文

我想开发一个 Phonegap 应用程序,并且我正在使用 jQuery Mobile。我正在 PC 上通过 Firefox 进行开发和测试,因此此处描述的问题与 Phonegap 没有任何关系 - 这是 Firefox PC 问题:

以下代码不起作用,我需要一些帮助来指出我的正确方向方向:

var loadWeather = function()
{
    // Request absetzen
    $.ajax(
    {
        // the URL for the request
        url : 'http://www.google.com/ig/api',

        // the data to send (will be converted to a query string)
        data : {
            weather : 'Vienna'
        },

        // whether this is a POST or GET request
        type : 'GET',

        // the type of data we expect back
        dataType : 'xml',

        // code to run if the request succeeds; the response is passed to the function
        success : function(xml)
        {
            parseXML(xml);
        },

        // code to run if the request fails;
        // the raw request and status codes are passed to the function
        error : function(xhr, status)
        {
            alert('Error retreiving weather!');
        }
    });
}

状态为“错误”,xhr.readyState=0,xhr.status=0,所以我根本没有从 jQuery 获得任何信息。请求被执行,应答标头(来自 Firebug)为:

Accept: application/xml, text/xml, */*; q=0.01
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Connection: keep-alive
Host: www.google.com
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1

和 200 OK。那么为什么我会跳入错误情况呢? xhr.isRejected() 说 true。这意味着什么?

I want to develop a Phonegap application and I am using jQuery Mobile. I am developing and testing via Firefox on the PC, so the issues described here don't have anything todo with Phonegap - this is an Firefox PC issue:

The following code does not work and I would need some help to point me in the right direction:

var loadWeather = function()
{
    // Request absetzen
    $.ajax(
    {
        // the URL for the request
        url : 'http://www.google.com/ig/api',

        // the data to send (will be converted to a query string)
        data : {
            weather : 'Vienna'
        },

        // whether this is a POST or GET request
        type : 'GET',

        // the type of data we expect back
        dataType : 'xml',

        // code to run if the request succeeds; the response is passed to the function
        success : function(xml)
        {
            parseXML(xml);
        },

        // code to run if the request fails;
        // the raw request and status codes are passed to the function
        error : function(xhr, status)
        {
            alert('Error retreiving weather!');
        }
    });
}

status is "error", xhr.readyState=0, xhr.status=0, so I don't get any info for the reason from jQuery at all. The request is executed, the answer-header (from Firebug) is:

Accept: application/xml, text/xml, */*; q=0.01
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Connection: keep-alive
Host: www.google.com
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1

and 200 OK. So why I am jumping in the error case? xhr.isRejected() says true. What does this mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2025-01-03 22:53:55

好的,这里发生了很多事情。首先,Firefox 不允许您使用 AJAX 进行跨域请求,因此当您从桌面浏览器尝试时会收到错误情况。

如果您在设备上进行测试,我怀疑 jQuery 会给您一个成功的结果。您会看到,在移动设备上运行来自 file:// 协议的代码时,同源策略不适用。事实上,我已经在使用 PhoneGap 开发的示例应用程序中对确切的 Google API 进行了 AJAX 查询。

但是,根据您使用的 jQuery 版本,可能存在错误。通常,当您从 file:// 协议发出 AJAX 请求时,返回的 xhr.status 将为“0”。这实际上没问题,应该与“200”一样对待,但我相信旧版本的 jQuery 在 0 状态方面存在问题。

我写了一篇关于从 PhoneGap 应用程序执行 XHR 的快速博客文章,您可以阅读:

http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html

它需要jQuery 完全脱离了这个等式。如果这不起作用,那么什么都不起作用。

Okay, so there are a number of things going on here. First Firefox won't allow you to do a cross domain request using AJAX so that is why you are getting the error case when you try it from your desktop browser.

If you were testing on the device I would suspect that jQuery would be giving your a successful result. You see when running code from the file:// protocol on a mobile device the same origin policy does not apply. In fact I've done an AJAX query to that exact Google API in a sample app I developed with PhoneGap.

However, depending on the version of jQuery you are using there may be a bug. Frequently when you do an AJAX request from the file:// protocol the xhr.status coming back will be '0'. That is actually okay and should be treated the same as a '200' but I believe older version of jQuery have an issue with the 0 status.

I've written a quick blog post on doing XHR from a PhoneGap application that you can read:

http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html

It takes jQuery completely out of the equation. If that doesn't work then nothing will.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文