jQuery Mobile 301 Redirect Issues

David Stinemetze picture David Stinemetze · Mar 22, 2012 · Viewed 7.2k times · Source

I am using jQuery 1.6.4 with jQuery Mobile 1.0.1. I am running into an issue anytime you link to a page that then tries to do a 301 redirect.

I've setup a sample page at: http://www.widgetsandburritos.com/jquery-mobile-test/

The only thing on this page is the jQuery Mobile includes and a link to another page that has a 301 redirect somewhere else.

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
    </head>
    <body>
        <a href="301test.php">301 test</a>    
    </body>
</html>

301test.php has the following content:

<?php
header( "HTTP/1.1 301 Moved Permanently" ); 
header( "Location: 301success.html" ); 
?>

This should just simply pass the browser to 301success.html. It works if you directly go to that URL

http://www.widgetsandburritos.com/jquery-mobile-test/301test.php

But when you click on the link from the page using jQuery Mobile, it shows "undefined" instead. Is jQuery Mobile currently incapable of handling redirects?

Any possible work arounds?

Thanks for your help.

EDIT [3/23/12 12:41AM CST]

I also posted this problem on the jQuery Mobile forums. Somebody there recommended adding rel="external" to the anchor tag. This technically works if all you are doing is making a link, but won't fix the issue if you get to the redirect via some other mechanism, such as a POST request.

To illustrate, I've setup a secondary test at http://www.widgetsandburritos.com/jquery-mobile-test/test2.html

<!DOCTYPE html>
<html>
        <head>
                <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
                <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
                <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
        </head>
        <body>

                <form method="post" action="301test.php">
                        <input type="submit" value="test" />
                </form>

        </body>
</html>

Instead of arriving at the 301test.php redirect page from a link, it's now the location of a form we're submitting to. The context this would be used, would be such that if you submit a form with errors, it would stay on the same page allowing you to correct the errors. If there were no errors, it redirects you to a success page. This is done to avoid submitting the form again if a user refreshes their browser. It works brilliantly in normal web applications. But in combo with jQuery Mobile it doesn't seem to work.

Just thought I'd give some additional context to anyone else following this issue.

Answer

David Stinemetze picture David Stinemetze · Mar 23, 2012

Figured out the answer to my own problem. In the above, I mentioned that this was causing problems using the <form> tag. After browsing through the jQuery Mobile documentation I found this page: http://jquerymobile.com/test/docs/forms/forms-sample.html

The trick here is if you're doing a form, to force it to not use AJAX. You do this by adding

data-ajax="false" to the FORM tag.

So this changes

<form method="post" action="301test.php">

to

<form method="post" action="301test.php" data-ajax="false">

And just to reiterate what was said above. If you need to do something with an anchor link, just add rel="external" to it.

So this changes

<a href="301test.php">301 test</a> 

to

<a href="301test.php" rel="external">301 test</a>