Redirect Cross-Domain AJAX Request in PHP

2

I have code that redirects an AJAX request and it works normally on localhost , but on the cross-browser server the request is not redirected.

header('Location: http://dominio.com/endereco');
exit;

Is it possible to block AJAX request redirection by default?

Is there any code to enable such redirection?

Can it contain an error in my code?

I make the request using jQuery.ajax:

$.ajax({
   url: "http://dominio.com/controller/action",
   data:dados,
   dataType: "json",
   success: Sucesso,
   error: Erro
})
    
asked by anonymous 14.03.2016 / 22:16

1 answer

1

You need to enable on your server that will be accessed by an external domain the permission for the external domain to access it. Considering http://api.dominio.com as your primary domain, and http://teste.api.dominio.com as your "external" domain, in index.php of http://api.dominio.com you will get:

header("Access-Control-Allow-Origin: http://teste.api.dominio.com");

Your main server ( http://teste.api.dominio.com ) also needs to return data in JSONP format, which is the extension of the JSON to allow Cross-domain request . And in your Ajax script use http://api.dominio.com as the expected extent:

$.ajax({
   url: "http://dominio.com/controller/action",
   data:dados,
   dataType: "jsonp",
   success: Sucesso,
   error: Erro
});
    
15.03.2016 / 21:33