How to enable cross-domain request on the server?
Solution 1
Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com
on target server
in php:
header("Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com");
in case you don't want to use server-scripting language: put this in (linux) console
a2enmod headers
and to your .htaccess file add
Header set Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com
Solution 2
Put this in your .htaccess and plain ajax works
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
Mahendra Liya
Updated on April 08, 2020Comments
-
Mahendra Liya about 4 years
I have a json file hosted on my server. When I try to make an Ajax "GET" request to the json file, it fails.
See the console in Safari, it says "Failed to load resource".
Firebug shows "200 OK", but the response doesn't show up. Even Firebug does not show the JSON tab.
I believe this is because Cross Domain Requests are not allowed using AJAX.
I would like to know how can I overcome this? Also, if I want to enable cross-domain requests on my server, I believe a
crossdomain.xml
file or something needs to be created. I am not sure, but this is what I know. I searched on Google, but could not find any relevant links.Any help in this is highly appreciated.
Thanks.
UPDATE: I am not using any server-side scripting language (PHP, ASP.NET, etc). I am using Plain HTML and JavaScript / jQuery.
UPDATE-2:
I used the following code to make cross-domain requests:
<script src="jquery-1.6.2.js"></script> <script> $(document).ready(function () { $.ajax({ dataType: 'jsonp', data: '', jsonp: 'jsonp_callback', url: 'http://myhosting.net/myjsonfile.json', success: function (jsonData) { alert("success") alert(jsonData); }, error: function(errorObj) { alert(errorObj.statusText); }, }); });
When i see in Firebug's "Net" tab, I see a JSON tab, and I am able to see the json response. However, the "success" callback handler doesn't get called, but the "error" callback handler gets invoked and I get the alert saying
parseerror
.Any idea what could be wrong?
-
Mahendra Liya almost 13 yearsHi, thanks for your reply. I am not using any server side scripting language... rather using just plain HTML and JavaScript / jQuery... Can you modify your answer to meet this...?
-
genesis almost 13 yearsHTML does not allow to change sent Headers
-
Mahendra Liya almost 13 yearsany idea as to whether I can do some setting on my server or ask the hosting provider to allow such requests?
-
Mahendra Liya almost 13 yearsI'll test it and update you the results.. thanks for your time!
-
Mahendra Liya almost 13 yearsActually I am requesting the json file from a web-app. So in this case, how can I set the value for
Access-Control-Allow-Origin
.. Any idea? -
Mahendra Liya almost 13 yearsHi genesis: I updated my question. I tried JSONP to make cross-domain requests. Can you please check "Update-2" and tell me if you can help in this? Thanks in advance.
-
genesis almost 13 years@mahi: can you show me your json response? It would be better to accept my answer here and create new question for this purpose
-
Azam Alvi over 10 yearsif we want to add this header into an HTML then what should we do for it to solve cross domain error?
-
Roy Toledo about 10 yearsI Tried it but still getting "Blocked loading mixed active content ..." :(
-
cwhsu about 9 yearsadd your port number if you use a specific port
-
jumpjack over 5 yearshow can I limit remote access to one single file but from any site?