dtkwt62022 2014-10-15 12:56
浏览 251
已采纳

如何基于jquery-ajax响应在同一窗口中打开新页面

This might be easy but I'm having trouble doing this right. How can I open a new page in the same window using jquery-ajax response?

As you can see I'm trying to display a custom text if the php response is equal to the word "invalid". If not open a new html page in the same window.

   $("#lBtn").click(function(){
      $.post("php/session.php",
        { username:$("#username").val(), 
          password:$("#password").val()
        },
        function(response) {
          alert(response)
          if(response == "invalid"){
             $("#loginResponse").text("Error!!");
          } else {
             window.location = $(this).find('new/pahe.html').html();
            }
        });
    });

Alert(response) shows the word "invalid" but the if statement does not work. Means neither it display Error!! in $("#loginResponse") nor it opens the new page. That's what I want to fix.

  • 写回答

2条回答 默认 最新

  • duanbanzhi4419 2014-10-15 13:33
    关注

    You are probably getting additional white-space returned in the output from the post (maybe a carriage return or space?).

    Trim the response:

    function(response) {
        response = $.trim(response);
        if(response == "invalid"){
    

    or just do this to check for a sub-string match:

    function(response) {
        if(response.indexOf("invalid")>=0){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?