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

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

问题描述

我想开发一个Phonegap应用程序,我使用jQuery Mobile。我正在通过Firefox在PC上开发和测试,所以这里描述的问题没有什么要做的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!');
        }
    });
}

状态为error,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()说true。这是什么意思?

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版本可能是一个bug。通常当您从文件://协议执行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天全站免登陆