Error response (status: 0) using ajax in iPhone

6,928 views
Skip to first unread message

user123

unread,
Feb 3, 2012, 3:52:22 PM2/3/12
to phonegap
My code is already working well as desktop web app and as PhoneGap app
for Android. I'm using jquery-1.7.1.min.js in both cases.

Now I'm testing on iPhone Emulator, using xcode, and ajax doesn't
work.

My call is this (url and data contain values I'm not displaying):
$.ajax({
url: https://xx.xx,
type: 'POST',
dataType: 'json',
data: data,
success: onSuccess,
error: function(xhr, textStatus, errorThrown) {
console.log(getErrorResponseString('Error response
sendFirstTimeEvalRequest', xhr, textStatus, errorThrown));
},
beforeSend: setHeader
});

function getErrorResponseString(myText, jqXHR, textStatus,
errorThrown) {
return myText + '\n' + 'XHR status: ' + jqXHR.status + '\n' + 'XHR
statusText: ' + jqXHR.statusText + '\n' + 'textStatus: ' + textStatus
+ '\n' + 'errorThrown: ' + errorThrown + '\n';
}

The error handler is being called, and I get this:
XHR status: 0
XHR statusText: error
textStatus: error
errorThrown:

Note: this is a cross-domain request. On desktop browser I have to use
a proxy for this to work. But I read same origin policy is not an
issue with mobile, so I'm sending the request to the URL directly. In
Android version everything works perfectly. I have no idea what this
problem could be. I read somewhere else that it could be that the
request is actually not being sent to the server. But I still have no
idea what it could be.

Any ideas? Thanks in advance!

Simon MacDonald

unread,
Feb 6, 2012, 9:36:19 AM2/6/12
to phon...@googlegroups.com
This is a common question on the list. Pasting in an old answer that should fit the bill:

"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 multiple domains 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."

Simon Mac Donald
http://hi.im/simonmacdonald



--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en

For more info on PhoneGap or to download the code go to www.phonegap.com

Ivan Schuetz

unread,
Feb 6, 2012, 10:05:08 AM2/6/12
to phon...@googlegroups.com
Hi Simon,

my problem was actually that I'm calling https and they use a self
signed certificate. The IPhone emulator doesn't trust it and that's
why it doesn't communicate. Installing a proxy with signed SSL
certificate worked.

It would be good if you post that in the group, maybe it helps other people.

Kind regards,
Ivan

2012/2/6 Simon MacDonald <simon.m...@gmail.com>:


> This is a common question on the list. Pasting in an old answer that should
> fit the bill:

>oes

Patrick

unread,
Feb 28, 2012, 11:17:58 AM2/28/12
to phonegap
Simon,

I tried to use your code in xcode and added the "*.twitter.com" into
the whitelist. I get a status=0, but nothing is in my responsetext?
Could it be that I don't have the correct phonegap (objective-c) code.
I move the .js from an android www to this mac.

Any help would be appreciated.

Patrick


Simon MacDonald

unread,
Feb 28, 2012, 8:05:52 PM2/28/12
to phon...@googlegroups.com
The phonegap.js is platform dependent so if you just copied it from
the Android version you will have a problem.

Also, make sure you are looking at "responseText" as the case is important.

Patrick

unread,
Mar 1, 2012, 12:27:44 PM3/1/12
to phonegap
It is looking at "responseText" with no success. I'm going to ask
someone at work, if they can reload phonegap on the MAC (xcode).

Pat


On Feb 28, 8:05 pm, Simon MacDonald <simon.macdon...@gmail.com> wrote:
> The phonegap.js is platform dependent so if you just copied it from
> the Android version you will have a problem.
>
> Also, make sure you are looking at "responseText" as the case is important.
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald

Patrick

unread,
Mar 26, 2012, 7:18:09 AM3/26/12
to phonegap
I found the answer on the stackoverflow website. It would be nice if
this was an option when you build the phonegap project.

http://stackoverflow.com/questions/9276643/ajax-https-call-in-ios-using-phonegap-not-working/9843315#9843315

Did you try to add this at the end of you AppDelegate.m file

@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end

Lucasf1

unread,
Mar 30, 2012, 5:14:58 PM3/30/12
to phonegap
Thank you, this solve my problem :)

Lucas

On 26 mar, 08:18, Patrick <pgwhitting...@gmail.com> wrote:
> I found the answer on the stackoverflow website. It would be nice if
> this was an option when you build the phonegap project.
>
> http://stackoverflow.com/questions/9276643/ajax-https-call-in-ios-usi...
Reply all
Reply to author
Forward
0 new messages