Phonegap jQuery ajax 请求不起作用 [英] Phonegap jQuery ajax request does not work

查看:37
本文介绍了Phonegap jQuery ajax 请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

和 200 OK.那么为什么我会跳入错误案例呢?xhr.isRejected() 说的是真的.这是什么意思?

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

推荐答案

好的,这里有很多事情要做.首先,Firefox 不允许您使用 AJAX 进行跨域请求,这就是您在桌面浏览器中尝试时遇到错误情况的原因.

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.

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

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.

但是,根据您使用的 jQuery 版本,可能存在错误.通常,当您从 file://协议执行 AJAX 请求时,返回的 xhr.status 将为0".这实际上没问题,应该被视为200",但我相信旧版本的 jQuery 有 0 状态的问题.

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.

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

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

它使 jQuery 完全脱离了等式.如果这不起作用,那么什么都不会.

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

这篇关于Phonegap jQuery ajax 请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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