Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

cordova - Phonegap jQuery ajax request does not work

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...