How to enable cross-domain request on the server?

78,672

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>
Share:
78,672
Mahendra Liya
Author by

Mahendra Liya

Updated on April 08, 2020

Comments

  • Mahendra Liya
    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
    Mahendra Liya almost 13 years
    Hi, 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
    genesis almost 13 years
    HTML does not allow to change sent Headers
  • Mahendra Liya
    Mahendra Liya almost 13 years
    any idea as to whether I can do some setting on my server or ask the hosting provider to allow such requests?
  • Mahendra Liya
    Mahendra Liya almost 13 years
    I'll test it and update you the results.. thanks for your time!
  • Mahendra Liya
    Mahendra Liya almost 13 years
    Actually 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
    Mahendra Liya almost 13 years
    Hi 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
    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
    Azam Alvi over 10 years
    if we want to add this header into an HTML then what should we do for it to solve cross domain error?
  • Roy Toledo
    Roy Toledo about 10 years
    I Tried it but still getting "Blocked loading mixed active content ..." :(
  • cwhsu
    cwhsu about 9 years
    add your port number if you use a specific port
  • jumpjack
    jumpjack over 5 years
    how can I limit remote access to one single file but from any site?