0

I am trying to send data to form site1 from site2 . But I get errors and the answer does not come back , what am I doing wrong? This is code ajax POST

  $.ajax({
  url: "site1/form.php",
    crossDomain: true,
    type: "POST",
    data: {a: 'a1'},
    contentType: "application/json; charset=utf-8;",
    async: false,
    dataType: 'jsonp',
    success: function(data){
        console.log('callback success');
        console.log(data);
    },
    error: function(xhr, status, error) {
        console.log(status + '; ' + error);
    }
});

and this is form from site1

$a= $_POST['a']; echo $a;

What could be wrong? Titles on site1 in .htaccess I ordered ..

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Seintero
  • 23
  • 5

2 Answers2

0

In cross domain ajax request modern browser send a preflight request to domain before making a actual request with method option. If you define custom header (in your case you have defined content-type) than it will be blocked. You can allow all request or particular domain in your remote server to accept all type request(in apache you can do it in htaccess and in IIS server you have to change webconfig.xml .

Karan
  • 2,102
  • 2
  • 22
  • 31
0

For obvious security reasons. Refer CORS. Cross-domain ajax request are forbidden by default, you can enable them, but it's a risk, as any other site can access your data on behalf of their users.

Do refer to this answer for security reasons.

Community
  • 1
  • 1
1binary0
  • 93
  • 11