Redirect AJAX Cross-Domain Request in PHP

Asked

Viewed 773 times

2

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

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

Blocking request forwarding AJAX is standard?

Is there any code to enable such redirection?

May contain 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
})
  • These are different domains, where you are running ajax and redirect domain?

  • because you don’t use JS to redirect, location.href ='http://dominio.com/endereco'

  • Make PHP return by JSON or the URL value only so that the client-side link is redid

  • These are different sub-domains, for example: http://api.dominio.com and http://teste.api.dominio.com

  • I cannot redirect via Javascript because I am building an API, and this redirection is part of the API rule and not the consumer’s rule.

  • As far as I know, browsers block AJAX requests out of the site making the request. This is for security and you may have an exception if the URL is localhost. Try to confirm if this is not the problem, and see the code returned by the function in inspecting.

  • That’s probably it, I talked to a college professor and he commented that this could be a default setting for browsers, where a request cross-Domain cannot be redirected for security reasons.

Show 2 more comments

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 its main domain, and http://teste.api.dominio.com as its "external" domain, index.php of http://api.dominio.com you’ll get:

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

thus allowing http://teste.api.dominio.com perform your request Ajax.

Your main server (http://api.dominio.com) also needs to return data in format JSONP, which is the extension of the JSON format to allow the Cross-Domain request. And in his ajax script utilize JSONP as the expected extension:

$.ajax({
   url: "http://dominio.com/controller/action",
   data:dados,
   dataType: "jsonp",
   success: Sucesso,
   error: Erro
});
  • The Allow-Origin is already present in the code, because the redirect would work with jsonp and not with json?

  • @kaduamaral I may have misunderstood your question. I entedi that you are trying to get data from a server A invoking $.ajax() on a server B.

  • In theory that’s it, and I’m getting it. The problem is when I try to redirect the request, such as when the user’s authentication fails. From what I have tested and researched I think this is not possible, it seems that the browser itself interrupts the request when it receives the redirect header... :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.