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-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.
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-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.
Share Improve this question edited Aug 23, 2011 at 0:32 LazyOne 166k48 gold badges414 silver badges415 bronze badges asked Aug 22, 2011 at 17:42 josephmisitijosephmisiti 9,99412 gold badges57 silver badges72 bronze badges 1- There is nothing wrong with this approach – Prisoner ZERO Commented Aug 22, 2011 at 17:54
3 Answers
Reset to default 2What you are doing is pletely 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 pletely: 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?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745279608a4620216.html
评论列表(0条)