Reputation: 15668
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?
Upvotes: 0
Views: 10271
Reputation: 23273
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.
Upvotes: 7