JQuery ajax POST from local file to access cross domain not working

As the title says, I am trying to access (POST) by calling jQuery AJAX to the web url, http://host:port/... or http://localhost:8080/... from a local HTML file, c:\home.html . I can not make it work.

I did Google and also saw some questions here, but I can't get it to work. I need help here. Here is what I have tried so far.

  • dataType: jsonp
  • crossDomain: true
  • Setting the title in my answer:
 response.setHeader("Access-Control-Allow-Origin", "*"); 

None of the three browsers work - IE, FF or Chrome. The request never reaches the server. Here are some of the errors that I see.

  • No transport (IE) unless jsonp is used.
  • NS_BINDING_ABORTED / Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED) to FF

This is my code. I would be grateful for any help. I am using jquery-1.8.2.min.js .

 var http_host = "http://localhost:8080"; function su (pc, p) { var suUrl = http_host + "/ps/api/v2/authorize.json"; $.ajax({ type: 'POST', url: suUrl, data: { phone_cell: pc, password: p, }, dataType: "json", crossDomain: true, success: osu, error: oe }); return false; } function osu (d) { console.log(d); } function oe(xhr, ts, et) { alert("ServerError: " + et); } 

An example would be a perfect pointer.

+4
jquery ajax cors cross-domain
source share
2 answers

I assume that my code messed up with all the different solutions that I tried. Finally, I was able to get it to work with the header setting (a solution recommended and worked for others). All I needed to do to get it working was to add the following to my REST service response.

 response.setHeader("Access-Control-Allow-Origin", "*"); 

Update:

I thought I understood this, but I did not. This is more than just a header setting. In any case, in my specific situation. I tried to run the application (html, js) from the hard drive specifically on chrome and try to access the web services available in the cloud.

This is how I finally solved the problem. I ran chrome with options.

 --disable-web-security -โ€“allow-file-access-from-files 

As I mentioned earlier, this application is a desktop application that will run as part of an embedded frame.

Thanks to everyone for your input.

+4
source share

You cannot make a cross-domain request from a local file because it is not in a domain . You need to host C:\home.html on the local web server for it to work.

+1
source share

All Articles