0

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 HTTPMovedPermanently(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?

1
  • If possible, use mod rewrite for any redirects.
    – Calum
    Aug 8, 2011 at 9:11

1 Answer 1

0

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.

2
  • thank you for your response. this is actually not really a form - i have an autocomplete box. once the user selects a value, he should be redirected to the appropriate page. therefore, there is no <form action=> atribute. what should i do then? again, i can't use window.location because it is not SEO friendly.. how can i tell the browser to actually redirect?
    – Aviv
    Aug 8, 2011 at 16:23
  • Even if you do it the way you intend to do it, it won't be SEO friendly anyway. The crawlers won't try to type stuff in your auto-complete box. You should use window.location. If you want your site to be SEO friendly, just code as if your site was used by disabled people. Make sure every section of your site is accessible without this auto-complete box. If you have a link for every section somewhere, the crawlers will be able to find the pages and disabled people will love you. Aug 8, 2011 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.