AJAX origin null is not allowed by access-control-allow-origin

Ronan sistema picture Ronan sistema · Oct 17, 2013 · Viewed 7.3k times · Source

I am working with BASEX (an XML Database) and i am trying to connect to the http server for performing REST request using Ajax. My code work well in IE but doesn't work with Firefox or Chrome. I tried two ways for the ajax query.

Here is the first :

var jqxhr = $.get( "http://localhost:8984/rest/factbook?query=//city&wrap=yes", function(data)

This code works on IE but doesn't works on other browser, after some research I found that there is a problem with the origin:

origin null is not allowed by access-control-allow-origin

And the second :

$.ajax({
    type: "GET",
    url: "http://localhost:8984/rest/factbook?query=//city&wrap=yes",
    contentType: "xml/application",
    dataType: "xml",

This code works on IE and got a 404 error on other browser.

I tried to put my script on a wamp server but I got the same error :

origin http:/localhost is not allowed by access-control-allow-origin

I also tried to replace local host by the ip of the computer but I got the same error.

Can someone help me please ?

Answer

Aaron picture Aaron · Nov 7, 2013

I guess the reason you can not get it work is that some server side coding are needed.

Try this, add the Access-Control-Allow-Origin header to the HTTP response and set the value to *. Here's a piece of Java code for your information.

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