2

I was trying to POST my login credentials for one of my app built with Cordova and AngularJS. When I login from my browser, it works fine but when test the same in Android (also tested in iOS too, fails on both), the login fails and I get the following headers while debugging.

I saw the differences in these two headers, as in emulator the origin is Origin: file://. It also has a warning sign for emulator saying "Provisional headers are shown"

Why is it failing in emulator and devices but works fine in browsers? Does cordova does anything internally while wrapping up the POST call?

I really don't understand why is it happening in emulators/devices but works fine in browsers.

In Browser - works fine

Remote Address:xx.xx.x.xxx:xxxx
Request URL:http://myEndpoint
Request Method:POST
Status Code:200 OK

Request Headers view source

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:166
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:myHost:xxxx
Origin:http://myComputerName.org:xxxxx
Referer:http://myComputerName.org:xxxxx/myAppName/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36

Form Dataview parsed

grant_type=mypassword&client_id=1234&client_secret=1234&username=userName&password=mypassword

Response Headersview source

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://myComputerName.org:xxxxx
Content-Type:application/json;charset=UTF-8
Date:Thu, 12 Mar 2015 15:25:53 GMT
Server:Apache-Coyote/1.1
Set-Cookie:
Transfer-Encoding:chunked

======================================================================

In My Android Emulator --> Failed to login

Request URL:http://myEndpoint

Request Headers

Provisional headers are shown
Accept:application/json, text/plain, */*
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:file://
User-Agent:Mozilla/5.0 (Linux; Android 5.0; sdk_phone_armv7 Build/LRX09D) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:1234

Form Dataview parsed

grant_type=mypassword&client_id=1234&client_secret=1234&username=myUserName&password=mypassword
slackmart
  • 4,754
  • 3
  • 25
  • 39
jeewan
  • 1,597
  • 5
  • 23
  • 40
  • You should ensure you can authorize external urls in your application (in the configuration xml files). Also you should post not just that file but also the chunk of code executing the $http.post call. – Luis Masuelli Mar 12 '15 at 16:13
  • Hi Luis, I was using $resource service in AngularJS not $http, though this might not be the case here. However, can you please tell me in more details what should I make changes in config XML file or anything should I have to make changes. Thanks. – jeewan Mar 12 '15 at 16:16
  • I don't currently remember, but you MUST remember you cannot use local-absolute urls. You can query /mypoint/ with any method in browsers, but in mobile devices, /mypoint/ will be local to the in-device deploy, so the url schema will be file:// – Luis Masuelli Mar 12 '15 at 16:21
  • 1
    Your ajax call must ALWAYS use remote-absolute urls (e.g. https://www.example.org/myentry/ instead of /myentry/). Also you should allow www.mysite.com domain in the access control (I don't remember the exact mechanics on that file, check the cordova docs) – Luis Masuelli Mar 12 '15 at 16:23
  • Related: http://stackoverflow.com/questions/21297169/cors-and-phonegap-apps – mrak Mar 12 '15 at 16:25
  • PLEASE NOTE: the comment structure converted the url I wrote into a link, with text set to the same host and path but no schema. Copy the href, and you will see what am I talking about. – Luis Masuelli Mar 12 '15 at 16:29
  • My Ajax call have the full absolute URL but I am not a back end developer and not sure how the back end is expecting to get these POST data. But, I am really pulling my hair, why it is working in browsers and not in devices and emulators. Arrggg ... – jeewan Mar 12 '15 at 17:51
  • I have been given an end-point, using which works fine in browsers but fails in devices/emulators – jeewan Mar 12 '15 at 17:52

0 Answers0