Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax calls fail - is it because URL is http://phonegap-serve-ip and not file://some-local-file? #169

Closed
jornklung opened this issue Jun 20, 2014 · 20 comments

Comments

@jornklung
Copy link

Hi,
PhoneGap App Developer is a great concept - got it up and running.
However there seems to be an inherint problem with doing Ajax calls/requests.
We have an app that catches content from a server.
This works fine when running the app as an installed app as the URL is file://some-local-file
BUT when running it with PhoneGap App Developer the URL becomes the URL of the phonegap server which PhoneGap App Developer is connected to.
When doing the AJAX calls they get rejected due to what seems like cross domain requests.
Do you have any suggestion on how to solve this problem?
Thanx for a great product and service!
Jørn

@brianleroux brianleroux added this to the Backlog milestone Jun 21, 2014
@brianleroux
Copy link

yup we're working on a CORS fix and should have something soon

@jornklung
Copy link
Author

Great to hear! It is a great product and this would make it useful for testing full apps.
thanx.

@timkim
Copy link
Contributor

timkim commented Jun 23, 2014

Hey there,

We do have a fix in the latest version of https://github.com/phonegap/connect-phonegap for ajax calls. Perhaps you may need to update the node module to get it working?

@jornklung
Copy link
Author

Hi, I updated phonegap - I no longer get CORS type of exceptions, but it seems the ajax call just stays forever - $.active shows 0 before the call, and then for each new call increases with 1.
Setting timeout on the ajax call does not seem to work. For all I know it could be an issue with jquery ajax call counter (existing issue) - but there is no log of an exception in the console. The code works fine when running as a regular web page.
I'm using weinre to do remote "debugging"

@jornklung
Copy link
Author

I'm using phonegap 3.5.0-0.20.4 and running with: phonegap serve
Is this the correct version? Is there some other parameters I should add to phonegap serve?

I should add that if I build the app through phonegap and install it, then it works as expected.
So I'm inclined to think it has something to do with phonegap serve or dev app?

@timkim
Copy link
Contributor

timkim commented Jun 27, 2014

Hi again,

Hrm, you are running a version of phonegap which should work. I'm not too sure exactly why either at this point but it does seem like it could be the phonegap serve or dev app. Could you post a code sample?

@jornklung
Copy link
Author

I'll post example code, but it really is a very basic ajax call - similar to what is described in issue 51 in connect-phonegap - and when running from the app packaged with phonegap build, then it runs perfectly (on the same device and same network/url etc).

Adding some additional findings after logging the ajax call object.
readyState: 0
status: 0
state: rejected

Coud it be relating to: phonegap/connect-phonegap#51

@fjlaubscher
Copy link

Hi guys.

Just want to let you know that I'm having the same issue as @jornklung.
I've tested and I've seen that HTTP GET's are working 100% but my POST's just aren't going through. Eventually after some time the request times out and the phonegap serve console outputs my url followed by ECONNRESET. See below for a screenshot.

image

Tested on iOS7 and WP8

@mwbrooks
Copy link
Collaborator

@djfranzwa

We've published a patch for the ECONNRESET issue. Would you mind updating to the latest CLI version and giving it another shot?

Reference: phonegap/connect-phonegap#75

@mwbrooks mwbrooks modified the milestones: Backlog, Week #4 - Sync'r up Aug 26, 2014
@robsonselzelin
Copy link

Hello!

I'm glad to say that I've updated my Node.js to v0.10.31 and now I'm able to make ajax POST calls to my server, which wasn't working on v0.10.29, with phonegap serve.

I'm using phonegap 3.5.0-0.21.14 that combined with latest Node.js (v0.10.31), it creates a proxy to the ajax request and everything is working just fine. I'm testing my app written in a Windows 7 machine, running it on an iPhone. Amazing!

@fjlaubscher
Copy link

@mwbrooks everything works 110%

Thanks so much for doing an awesome job with phonegap (serve). You guys are the best.

@mwbrooks
Copy link
Collaborator

mwbrooks commented Sep 5, 2014

@robsonselzelin @djfranzwa great to hear guys! I'll leave this issue open until we roll out the next milestone, which should apply for more broad fix. Regardless, it's good to hear that the upgrade has helped you out!

@atif089
Copy link

atif089 commented Nov 5, 2014

Guys, I'm on v3.6.0-0.21.19 (and node v0.10.32) and still face this issue. Testing on Windows 7 (android platform)

My code is here -> http://stackoverflow.com/questions/26748796/phonegap-and-cors-xhr-status-0-issue

@wclr
Copy link

wclr commented Nov 30, 2014

The problem persists on latest verison 4.1.2, it is not proxing to the same ip on other port.

@jornklung
Copy link
Author

Hi, I've now finally gotten back to test this and it works great if the url is on a different ip/name - but it does not work for requests to different port but same ip as phonegap serve is running on - likely same isssue as @whitecolor

If I run phonegap serve on http://192.168.1.40:3000 then request to http://some.domain.com works fine, but request to http://192.168.1.40:8888 does not work.

What is odd is that requests on the same ip (but diff port) does not show any activity in the phonegap serve console, however the request reaches the target server but the response is not getting back.
For remote ip the log has something like: [phonegap] 200 /proxy/http//some.domain.com/
There is no trace of it when using same ip.

Hope this helps. Thanx for great improvements!

btw using:
phonegap 4.1.2-0.22.10
node: v0.10.34
win7x64

@souhailalavi
Copy link

Hi, We are developing a project using phonegap for iOS and Android.

One of the features we implemented depends on calling another page from within the phonegap project using ajax. This works fine on iOS and Android 4.0, however due to the Android Lolipop using Chrome as a default browser, now our links dont work and we get a CORS error.

XMLHttpRequest cannot load file:///Users/sou/login%20page/www/restodetails.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Android 5.0 was just realeased a few months ago and now we have to fix this issue. Has anymore come across an issuel ike this loading another file from within the app?

Please help!

@mwbrooks
Copy link
Collaborator

Hi @souhailalavi,

By the sounds of it, this isn't a PhoneGap Developer App issue (directly). I've just spoke with @infil00p and he suggested that this issue may have been resolved in the latest Cordova Android release, which coincidently came out on Friday. If you're building the Android app locally, then you can try to upgrade to Cordova Android 3.7.0, which apparently disables CORS by default. However, the PhoneGap Developer App is running Cordova Android 3.6.x, so you'll still experience the CORS issue with it.

You can install the latest Cordova Android with:

$ cd my-app/
$ phonegap platform add android@3.7.0
$ phonegap run android

Cheers,
Michael

@snow01
Copy link

snow01 commented Feb 20, 2015

I am also facing the exact same issue as @whitecolor and @jornklung mentioned above.

Probably it is an issue with phonegap developer app ?

@shohoku11wrj
Copy link

I wondering if there is any solution for simple Ajax cross site GET request in HTML5 app using PhoneGap?

$.ajax({
            url: "some api request",
            headers: {"Content-Type":"application/json; charset=UTF-8"},
            data: jsonData,
            dataType: "json",
            method: 'GET',
            success: function( data ) {
                $("#pname").html(data);
            }
        });

I have allow <access origin="*" />
and include js library needed:

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="cordova_plugins.js"></script>
<link rel="stylesheet" type="text/cs" href="jquery.mobile-1.4.5.min.css" />
<script type="text/javascript" src="jquery-1.10.0.min.js"></script>
<script type="text/javascript" src="jquery.mobile-1.4.5.min.js"></script>

@timkim
Copy link
Contributor

timkim commented May 20, 2015

With the 1.5.2 release, AJAX calls should now work like it normally does in a PhoneGap app.

@timkim timkim closed this as completed May 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests