简体   繁体   中英

Redirect to another page using php and ajax

I have two pages one for filling data and other one for initializing data I am sending the filled data via post method using ajax, I want to redirect the user from initializing page, i used simple header("Location: somelocation"); in the initialize page but it seems it does not work this way and it just sends the html value of the target page using ajax (I checked it with firebug),so what is the alternatives here ? should i use ajax it self for the redirection part?

You should return your location via json response, for example {"location": "somelocation"}, and then handle it in ajax success callback and go to page you need:

document.location.href = somelocation

You should use javascript for the redirection, not ajax.

When your ajax request has completed successfully you should then do a window.location = 'new/path/to/some/location';

Using the header("Location: ...") in your destination PHP script (the one that receives the data) will only return to you the html in the ajax response body, but not interpret it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM