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

如何基于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.

这可能很容易,但我在做这件事时遇到了麻烦。 如何使用jquery-ajax响应在同一窗口中打开新页面? p>

正如您所看到的,如果php响应等于单词,我正在尝试显示自定义文本 “无效”。 如果没有在同一个窗口中打开一个新的html页面。 p>

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

警报(响应)显示 单词“invalid”但if语句不起作用。 表示它既不在 $(“#loginResponse”) code>中显示 Error !! code>,也不会打开新页面。 这就是我想解决的问题。 p> div>

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){

EDIT: You did not word your question right. What you are aiming to do is to redirect to another page.

If #lBtn's html is the url itself, the solution to that is to use:

window.location.href = $(this).html();

This is for opening the link in a new tab

First create a new link, hidden somewhere:

Javascript code

$('body').append('<a id="new_wnd_temp" href="another_page.html" target="_blank" style="display:none"></a>');

Then trigger a click event on it:

Javascript code

$('#new_wnd_temp').trigger('click').remove();

In your example, it would look like this:

Javascript code

$("#lBtn").click(function() {
    $.post("php/session.php", {
        username:$("#username").val(), 
        password:$("#password").val()
    },
    function(response) {
        alert(response);

        if(response == "invalid") {
            $("#loginResponse").text("Error!!");
        } else {
            $('body').append('<a id="new_wnd_temp" href="another_page.html" target="_blank" style="display:none"></a>');
            setTimeout(function () {
                $('#new_wnd_temp').trigger('click').remove();
            }, 0);
        }
    });
});