duanlei1957 2011-09-08 12:02
浏览 230
已采纳

基于AJAX调用重定向到新页面

I am using ajax with jQuery to make a request from a page on my site. Now if the login failed (or the call failed for a different reason), I return an error message and put it in a label. But if the call succeeds, I want to navigate to another page. My issue is that if the call succeeds, I end up with the text of the new page in my label.

Here's my Javascript:

 $.post("chklogin.php", { username: username, password: password }, function(data) {
        $('#msg').html(data);
 });

And here's the PHP that it calls:

if(mysql_num_rows($result)==0) {
    $msg="<h3>Enter valid Username and Password.</h3>";
    echo $msg;
} else {
    $row=mysql_fetch_assoc($result);
    $_SESSION["userid"]=$row["pgmail"];
    header("location:user.php");
}
  • 写回答

1条回答 默认 最新

  • downloadbooks_2014 2011-09-08 12:15
    关注

    Well this issue could have been clearer, and I would encourage you to check your spelling and code before you post again.

    The issue is that you haven't sent the page to redirect, you have sent the page being called to redirect.

    To fix this issue:

    Firstly, get rid of the header redirect on your PHP code and replace with:

    echo 'SUCCESS';
    

    Secondly, change your AJAX code to the following:

     $.post("chklogin.php", { username: username, password: password }, function(data) {
          if(data=='SUCCESS'){
             window.location.href = "user.php";
          }else{
             $('#msg').html(data);
          }
     });
    

    This will make the page redirect, not the page called by AJAX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?