I have a Pyramid app for a web page with a form. I was using window.location = XXX after the form's POST but I was told that this is not SEO friendly.
Therefore, I am trying to do a redirect on the server side. What I did is add a view in my pyramid app at mydomain.com/redirect. Going to this address returns a 301 to the destination page:
return HTTPMovedPerma开发者_如何转开发nently(location=location)
Now, in my web page, I added a jQuery POST to the /redirect page. Looking at firebug, the post does indeed happen, it gets a 301 back, and then my browser GET's the destination page, but doesn't physically redirect to it - what am I missing?
jQuery won't redirect the browser when it receives a 3xx response. It will only follow the redirection in the AJAX request. It's the standard behavior for XmlHttpRequest and I am pretty sure it can't be changed.
What you want is either not submit your form with JavaScript and use a HTTP redirection, or use JavaScript and use window.location when it receives a positive response.
The usual way to do this kind of thing is assume the client does not have JavaScript first. Then once everything works without JavaScript, you add JavaScript over that and make it a better experience for the users that have JavaScript.
That said, if you intend to redirect the client after a form submition, you could simply not use AJAX. If you have validation to do, you can prevent form submition in JavaScript, until the form is valid.
Also, the status code for a redirect after a form is submitted is usualy 302, not 301.
精彩评论