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 POST request from phonegap to server fails. i use phonegap 4.2.0-0.24.2. #274

Open
pvin opened this issue Mar 12, 2015 · 14 comments
Open
Milestone

Comments

@pvin
Copy link

pvin commented Mar 12, 2015

Ajax POST request from phonegap to server fails. any idea to solve? or any other way to workaround?
i use phonegap 4.2.0-0.24.2.

@timkim
Copy link
Contributor

timkim commented Mar 19, 2015

Hi @pvin

Try this comment from a similar thread: #169 (comment)

@santinobonora
Copy link

hi @pvin ,
i have this same issue since the update with 19 may. i think this is because the new way of syncing .. i try to make an ajax call to my localhost but it dos not hit any breakpoints anymore

i dont know how to fix this, any help would make my day 😄

@pvin
Copy link
Author

pvin commented May 22, 2015

@santinobonora sorry i didn't try to fix....

@pvin
Copy link
Author

pvin commented May 22, 2015

@santinobonora
this may help u....
#169 (comment)

@santinobonora
Copy link

@pvin i saw it earlier yes, this did not fix my issue. but many many thanks already

@mwbrooks
Copy link
Collaborator

@santinobonora In order to make a XHR/ajax request to your localhost, you will need to hard code it's public address. This is likely the same IP address reported by the phonegap serve command. Previous version of the PG Developer App may have worked using relative URLs, but that is no longer the case.

@eveli050
Copy link

eveli050 commented Jun 1, 2015

My phonegap app was working just fine, till the moment I updated phonegap. Now, any changes that I make to the files don't refresh the app and none of the ajax requests work. I am running the app on localhost. It doesn't even print out console logs outside the ajax calls. All that is printed on terminal is sth like the following: [phonegap] 200 /socket.io/?EIO=2&transport=polling
Anyone have any suggestions?

@eveli050
Copy link

eveli050 commented Jun 1, 2015

When I compared my updated project to the one that has the older version of phonegap (on GIT), I noticed that the older version contains .cordova folder whereas the new one doesn't.

@santinobonora
Copy link

helle @eveli050 i have solved my problem with placing my webservice into a server. this solved the problem. localhost is not reachable anymore @mwbrooks to the ip adress did not work for me eighter.. thank you anyhow for helping me! like i said make a call to a server .. this could solve the problem for you.

@eveli050
Copy link

eveli050 commented Jun 2, 2015

@santinobonora many thanks

@theogray
Copy link

theogray commented Jul 2, 2015

Originally started a thread at phonegap/phonegap-app-desktop#375 but now convinced it's a Phonegap Developer issue rather than Desktop and found this related thread. The problem appears to have started with one of the v1.5.x releases.

  • Still a problem on Developer v1.5.4 iOS(8.4) / Desktop v0.1.7 Windows(8.1).
  • Developer v1.4.0 Android(4.4.2) works fine through same Desktop.

I have checked the IIS Express log files for the server side and the request does not appear to be getting as far as that when made from v1.5.x.

The response returned by $.ajax on (post) requests to localhost:45678 (have tried various ports above 40000) on v1.5.x is the same as if there was a CORS issue (i.e. {readyState:0, status:0, statusText:"error"}), but if it is, presumably it's a CORS issue within Phonegap Developer (unless Apple changed something in recent iOS release that might affect this).

Phonegap Desktop log is showing:

  • When Developer v1.4.0 on Android makes the request, two entries
    • 200 /socket.io/?EIO=2&transport=polling&t=1435845568844-2&sid=ZB1H09goREA65jRgAAAA
    • 200 http://localhost:45678/api/jobs
  • When Developer v1.5.4 on iOS makes the request, just a single entry
    • 200 /socket.io/?EIO=2&transport=polling&t=1435847041120-3&sid=_npNFQXxn7T2lQOmAAAA

When hitting live URLs (e.g. https://example.com/api/jobs) via Desktop v0.1.7 everything works fine. Origin header is being passed to those as file://

@mwbrooks
Copy link
Collaborator

mwbrooks commented Jul 7, 2015

Hey @theogray,

Thanks for the detail and your analysis!

The latest version of the PhoneGap Developer App runs on the file protocol and should by-pass single origin security policy (CORS).

It's concerning that the IIS Express server logs do not show the incoming request. In your previous thread, you mentioned that it is binding to 127.0.0.1 and it is not trivial to migrate to Full IIS. I'm not familiar with IIS Express, but a few quick Googles suggest that running it under Administrator allows you to bind to all available network adapter addresses. Coming from someone who's never used IIS, I'm not sure if that helps you.

A quick test to see if things should work would be take another computer on the same network as the IIS Express box. Open a desktop browser and try to ping the end-point (e.g. http://x.x.x.x/api/jobs). If that request appears in the IIS Express logs, then we certainly have an issue with the Developer app (assuming it's on the same network as well).

@theogray
Copy link

theogray commented Jul 8, 2015

Thanks @mwbrooks,

I've found the info you mean for IIS Express. Having tried that out I can see what the difference between v1.4.0 and v1.5.x is, as I also had to open the 45678 port up on my dev machine firewall (where that wasn't necessary before).

It looks like there has been a fundamental change to PhoneGap Developer since v1.4.0 - where before all requests (or at least those to localhost) were proxied via PhoneGap Desktop, now they are not and the call is being made directly from the device (which explains why calls to localhost fail).

This is a problem as the proxying made the dev environment very simple to setup - each dev runs everything locally without needing admin privileges and the only port that needed to be open on each dev machine firewall was 3000. Also all $.ajax requests go to the dev's localhost and only hit their setup rather than anyone else's.

Has this proxying been removed for a reason, or just been lost due to a different change? In a dev environment it makes a lot of sense to run things with all traffic going via PhoneGap Desktop, although for testing purposes I can understand that sending requests directly from the device may be preferable to some. Is this a feature that could be added back in and then controlled via a setting on the device or in config.xml?

@nabaasa
Copy link

nabaasa commented Jun 16, 2016

hi, to solve the issue..try to use this
i mixed cordova with jquery ajax
$(function(){
$(document).bind('deviceready',function(){

$.get(url,callback);

});

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

7 participants