0

I am having an ajax problem. I am doing cross domain, i.e I am submitting a form on another domain.

Here is my js code:

$.ajax({
    url: $(this).attr('action'),
    type: $(this).attr('method'),
    data: $(this).serialize(),
    dataType: 'json'
})
.done(function(data) {
    console.log('what');
})
;

and in php I am returning this string:

$response = "Hmmm... Please contact the webmaster at contact@vahana.io as it seems that there is a problem with submitting this form. Error x02";
echo json_encode(['response' => $response]);

I did understood from my searches that I should in my dataType use jsonp instead of json, but with jsonp I cant even submit my form.

When I submit my form with json, the data is properly inserted in my DB but I cant get any response. I tried the .success instead of .done but does not work either.

My first question, can I do cross domain with json or not.

If I need to use jsonp, how do I check in my browser that everything is going right, and how should I modify my response because right now if I use jsonp I am having an error in the console because of the ':' which seperate the key from the value.

Thanks for your help

khalid sookia
  • 317
  • 2
  • 13

1 Answers1

0

You have set the headers also in your php in such a way that it allows you to accept request from other domains also.

<?php

  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Methods: GET, POST');
  header("Access-Control-Allow-Headers: X-Requested-With");
  $response = "Hmmm... Please contact the webmaster at contact@vahana.io as it seems that there is a problem with submitting this form. Error x02";
  echo json_encode(['response' => $response]);

?>

Detailed explanation is given here :
(1) CORS with php headers
(2) http://www.w3.org/TR/cors/
(3) https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Community
  • 1
  • 1
Sony Mathew
  • 2,929
  • 2
  • 22
  • 29