开发者

function does POST and GET request in firebug, but no header redirect

开发者 https://www.devze.com 2023-02-20 18:33 出处:网络
I have a form that submit an request with jquery $(\"form input[type=submit]\").live(\"click\", function(event){

I have a form that submit an request with jquery

$("form input[type=submit]").live("click", function(event){

    event.preventDefault();
    //$(this).attr("disabled", "true"); 

    var postdata = $(this).parents("form").serialize(); 
    //console.log(data);

    $.ajax({
        type: "POST", 
        url: "../inc/process.inc.php", 
        data: postdata          
    });     

}); 

This cause the user not to be redirected to action="inc/process.inc.php" method="post" when clicking the submit button.

process.in.php

$host = $_SERVER["HTTP_HOST"]; 
$actions = array(
        'user_login' => array(
                'object' => 'Intro', 
                'method' => 'processLoginForm', 
                'header' => 'Location: http://'.$host.'/homepage.php'   
            )
    );

/*
 * Make sure the anti-CSRF token was passed and that the
 * requested action exists in the lookup array
 */
if ( isset($actions[$_POST['action']]) ) 
{
    $use_array = $actio开发者_JS百科ns[$_POST['action']]; 

    $obj = new $use_array['object']($dbo); 

    if ( true === $msg=$obj->$use_array['method']() )
    {           
        header($use_array['header']);
        exit;
    }
    else
    {
        // If an error occured, output it and end execution
        die ( $msg );
    }
}
else
{
    // Redirect to the main index if the token/action is invalid
    header("http://" . $host);
    exit;
}

When my method return true, this should cause the user to be redirected to

Location: http://'.$host.'/homepage.php,

but instead firebug gives me

POST http://192.168.1.133/homepage.php

and returns the content of the page in firebug, then a

GET http://192.168.1.133/homepage.php

with a blank response


Header will not work the way you are expecting it to work. The header instruction will redirect the XML HTTP Request object, not the browser.

EDIT

You need to improvise. Do something along these lines:

$.ajax({
    type: "POST",
    url: "../inc/process.inc.php",
    data: postdata,
    dataType: "json",
    success: function (data) {
        if (data.errorMessage) {
            alert(data.errorMessage);
        }
        if (data.redirectTo) {
            window.location = data.redirectTo;
        }
    }
});

In your php code:

// replace the following line:
//     header($use_array['header']);
//     exit;
// with
echo json_encode(array(
    "redirectTo" => $use_array["header"]
));
exit;

// replace the following line:
//     die( $msg );
// with

echo json_encode(array(
    "errorMessage" => $msg
));
exit;

// and so on


In the last else, try using header("Location: http://" . $host);

Regarding your problem, you perform an ajax request, and expect the whole browser to be redirected. Ajax dosent work like that. If you perform an ajax request, then only that request will get redirect, not the parent page/window, and even that is optional. You should either return a script tag <script>window.location='the new location'</script> or dont submit via ajax, and the things flow the "old school" way so the browser will pick up the header you are sending:)

0

精彩评论

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