1

I want to send a cross domain ajax request but I am getting below error for this I have also tried the below code which I have got from the one stack article is it possible to send a cross domain request or not.

Code which I have tried for sending cross domain request.

$(document).ready(function () {
    $.ajax({
        type: 'POST',
        url: "http://testdomain.com/config.php",
        crossDomain: true,
        data: 'rejected_by_loid=' + 1,
        success: function (msg) {
            $('#survey').html(msg);
        }
    });
});

Error on console log:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://beta.paravey.com/paraveyads/config.php. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Please help me to solve this problem.

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
varun joshi
  • 461
  • 1
  • 8
  • 27
  • You need to allow cross-domain ajax access on the server. Ask your system administrator to allow your connections to that server. – Peon Feb 01 '17 at 09:17
  • Do you control http://beta.paravey.com/paraveyads/config.php? If so, add the header to the response. If not, you need to look for a different solution, for example an ajax post to your server and then a curl request from your server to http://beta.paravey.com/paraveyads/config.php. – jeroen Feb 01 '17 at 09:18

2 Answers2

3

You must define trusted domain names in http://testdomain.com/config.php:

header('Access-Control-Allow-Origin: http://requested-domain.com', false);
header('Access-Control-Allow-Origin: http://requested-domain2.com', false);

The last parameter false disable overriding same header.

Of course if you want to open your domain for all calls you can set:

header('Access-Control-Allow-Origin: *');
Taron Saribekyan
  • 1,360
  • 7
  • 13
  • This is actually a better answer - you *do* want to restrict the domains allowed to reduce the (albeit small) security risk : http://stackoverflow.com/questions/12001269/what-are-the-security-risks-of-setting-access-control-allow-origin – CD001 Feb 01 '17 at 09:35
2

put it on top of config.php

 header('Access-Control-Allow-Origin: *');  
Indresh Tayal
  • 276
  • 1
  • 11