PHP header()函数犯规重定向AJAX调用后,函数、重定向、PHP、header

由网友(别涉世太深)分享简介:我使用AJAX来插入数据。登记后,如果成功的话,将重定向到它的注册表页面或registry.php。但它似乎不是重定向到相应页面,奇怪的是,当我检查在Firefox在控制台上看来,它给出了一个正确的结果,这样的事情POST XXXX / form_zuri / insertData.php 302找到50毫秒和GET...

我使用AJAX来插入数据。登记后,如果成功的话,将重定向到它的注册表页面或registry.php。但它似乎不是重定向到相应页面,奇怪的是,当我检查在Firefox在控制台上看来,它给出了一个正确的结果,这样的事情POST XXXX / form_zuri / insertData.php 302找到 50毫秒和GET XXXX / form_zuri / registry.php 302找到37ms。

I am using ajax to insert data. after registration if successful it will redirect to its registry page or registry.php. But it seems that it is not redirecting to its respective page and the weird thing is when i check it on the console on firefox it seems that it gives a right result, something like this POST xxxx/form_zuri/insertData.php 302 Found 50ms and GET xxxx/form_zuri/registry.php 302 Found 37ms.

继承人的$ C $下insertData.php

heres the code for insertData.php

    if($data = $admin->insertRegistrantInfo($fname, $lname, $aname, $email, $password) == true) {
    session_regenerate_id(true);// destroying the old session id and creating a new one
    $_SESSION['id'] =  $data;
    header('location: registry.php');
    exit();

我的继承人AJAX脚本:

heres my ajax script:

$("#submit_third").click(function(){


    $.ajax({
        type: "POST",
        url: "insertData.php",
        data: {

            fname                       : $("#fname").val(),
            lname                       : $("#lname").val(),
            aname                       : $("#aname").val(),                
            email                       : $("#email").val(),
            password                    : $("#password").val()              

            },
        cache: false,
        success: function(data) {

        //window.location.replace('registry.php') ;   

        }
    });
    //window.location.replace('registry.php') ;  
    //return false;

});

如果我将使用.replace js函数它不会是能够得到会话ID。

if i will use the .replace js function it wont be able to get the SESSION ID.

任何想法家伙?

推荐答案

当你调用一个PHP脚本的AJAX,请求正在由AJAX调用执行,而不是当前的页面在客户端上,因此也不会改变,实际上是被查看的网页上的报头 - 仅在AJAX调用的响应。在这种情况下不会做你想要的。

When you call a PHP Script in AJAX, the request is being executed by the AJAX call and not the current page the client is on, therefore it won't change the headers on the page that is actually being viewed - only on the response of the AJAX call. Which in this case won't do what you want.

另外,你不能改变头时,内容已经被输出而这又在你的情况下,它会一直。

Also, you can't change the headers when content has already been outputted which again in your case it will have been.

如果您在AJAX回调使用 window.location.href ='registry.php',会话ID应该在那里。使用 window.location.replace 将不是一个新的记录添加到您的历史记录。

If you use window.location.href = 'registry.php' in your AJAX callback, the session id should be there. Using window.location.replace won't add a new record to your history.

阅读全文

相关推荐

最新文章