1

I'm creatring a cordova-appache application where I communicate with a web server (get and post http request to http://192.168.1.1/cgi/json/****) in my code javascript I'm using xhr , when I send the first get request, the server respond me with json response and give me a session id and I follow response with wireshark, the fisrt problem http.status return 0 the second problem in the console log i had this message Reason: CORS header 'Access-Control-Allow-Origin' missing.

I added this in config.xml

<allow-navigation href="http://*/*"/>
    <allow-navigation href="https://*/*"/>
    <allow-navigation href="data:*"/>

And this to javascript code :

http.open('GET', request_field, true);
        http.setRequestHeader("Access-Control-Allow-Origin", "*");
    http.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    http.setRequestHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE, PUT, HEAD");
        http.send();

Like environment i'm using: ubuntu cordova version: 6.1.1 nodejs version :v0.10.25 and Firefox

mntsr
  • 31
  • 4
  • `` in `config.xml` – Rayon Apr 04 '16 at 10:47
  • done but doesn't work!!! – mntsr Apr 04 '16 at 11:05
  • Are you sending headers from your `webservice` ? – Rayon Apr 04 '16 at 11:06
  • I'm sending headers, like that: http.setRequestHeader("Access-Control-Allow-Origin", "*"); http.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); http.setRequestHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE, PUT, HEAD"); http.setRequestHeader("Connection" : "keep-alive"); http.setRequestHeader("Cache-Control" : "max-age=0"); http.setRequestHeader("Accept-Encoding": "gzip,deflate,sdch"); http.setRequestHeader("Accept-Language": "en-US,en;q=0.8"); http.setRequestHeader("Cookie": "lang=fr"); – mntsr Apr 04 '16 at 11:20
  • Your server will have to tell client that data is from valid source and allow this origin to serve the data..It must be there in your server side script.. – Rayon Apr 04 '16 at 11:23
  • I haven't the right to add this to my server – mntsr Apr 04 '16 at 11:25
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – Rayon Apr 04 '16 at 11:26

1 Answers1

0

Your setting in config.xml is effect when you build and run it in mobile. For development you can use my addon: https://addons.mozilla.org/en-US/firefox/addon/cross-domain-cors/?src=ss to enable cross domain and run your app in firefox.

Tan Mai Van
  • 579
  • 5
  • 7
  • hi Tan Mai Van, i have the same problem,CORS header 'Access-Control-Allow-Origin' missing – mntsr Apr 04 '16 at 12:41
  • i have the same message:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.1.1/cgi/****. (Reason: CORS header 'Access-Control-Allow-Origin' missing). – mntsr Apr 04 '16 at 13:26
  • Which version of firefox you are usinng? – Tan Mai Van Apr 04 '16 at 13:51
  • firefox version: 44.0.1 – mntsr Apr 04 '16 at 14:01
  • if that can you try to update to 45, latest version of firefox. It has some issue about cross domain with previous version. Try update firefox first to see if it can fix your problem. – Tan Mai Van Apr 04 '16 at 14:03
  • hi Tan Mai Van , i updated firefox but i have the same problem cross origin!! – mntsr Apr 04 '16 at 16:40
  • Hum, sound strange. Could you try to add this your server address to the add on: 192.168.1.1 Just to make sure it enable CORS in when you request to that server. – Tan Mai Van Apr 05 '16 at 14:54