开发者

Client-side redirect question

开发者 https://www.devze.com 2023-03-29 17:05 出处:网络
I am setting up a site now that uses a lot of ajax interactions. The basic flow of what is going on now is data is validated both client-side and server开发者_如何学Go-side, and if the data is correct

I am setting up a site now that uses a lot of ajax interactions. The basic flow of what is going on now is data is validated both client-side and server开发者_如何学Go-side, and if the data is correct the response from and AJAX post will be the url to the next page to go to.

When I receive the url client side I am currently using window.location = url to perform my redirect.

For some reason, something about this seems incorrect. Specifically, looking through a variety of open-source projects, I rarely see it. I am wondering if the interaction I am describing above is correct, and if it is not, what I could do differently to correct my current design.


What you are doing is completely fine and is the normal approach that people take. Something to keep in mind is how do you handle failed requests that is your post failed for some reason or there was failure in the ajax request etc.

There was an interesting so question and read the marked answer: How to manage a redirect request after a jQuery Ajax call

Hope this helped


The best way for redirections is from the server side, basically because the server is the one who knows if you can access this page or not and response 302 redirection in the header or just return the page it self, then if the server approve your request for the page the AJAX should handle the interaction in the page without taking concern about the security.

If anyway you don't concern about security then window.location is fine.


This defeats the whole point of AJAX: avoid loading full pages for every interaction, and thus limit bandwidth usage and be more responsive.

With your design, each interaction needs two requests: one to validate the data, and a second one to load the next full page. You'd better avoid AJAX completely: it would certainly be more efficient. Why not returning an error response if validation fails, and a success response with what must be displayed after the post if validation succeeds?

0

精彩评论

暂无评论...
验证码 换一张
取 消