1

I am checking if username exists live from the database if it is the username is not available, the response is Inavailable and vice versa. Am getting this response successfully but I cannot match this response string so that I display different details to the user. This is my Jquery Ajax function

var dataString = 'user_name=' + username;
$.ajax({
    type: "POST",
    url: "ajax_scripts/checkUser.php",
    data: dataString,
    cache: false,
    success: function(response) {    
        if(response=="Available")
        {                                                                                                                                                                                                                                                                                                                                                                                                                            
            alert("The server data matched"); 
        }
        else if(response=="Inavailable") {                                                                                                                                                                                                                                                                                                                                                                               
            alert("The server data did not match"); 
        }
    }
});

I cannot match the server resposne. Kindly assist me i am beggining to learn jquery ajax

4

3 回答 3

1

检查您从服务器得到的答案:

success: function(response) {    
   console.log(response);
   if(response=="Available"){  
      ....

如果服务器的响应包含尾随空格或换行符,则测试if(response=="Available")将不正确 - 一个常见的 PHP“错误”:在响应中发送结束标记后的额外空格...

如果您负担得起,我建议您让服务器返回 Json 响应:

//PHP (server side) :
... code ...

$response = new stdClass();
$response->available = $usr != null; //insert correct test here
// add other data if needed :
// $response->userName = $usr ? $usr->Name : "";
// ...

header('Content-type: application/json')
echo json_encode($response);    

//Javascript (client side) :
$.ajax({
  type: "POST",
  url: "ajax_scripts/checkUser.php",
  data: dataString,
  cache: false,
  dataType: 'json', // <- add this option
  succes: function(data) {
    //with "json" dataType, data will be a regular javascript object
    //built from the server's response
    if (data.available) {
        alert("is available");
    } else {
        alert("not available");
    }
  }
}
于 2013-10-08T07:19:27.660 回答
0

它发生在某些情况下解决方案是修剪您的响应并匹配它尝试下面的代码。我刚刚使用了 jquery 修剪 $.trim();

var dataString = 'user_name=' + username;
$.ajax({
    type: "POST",
    url: "ajax_scripts/checkUser.php",
    data: dataString,
    cache: false,
    success: function(response) {    
        if($.trim(response)=="Available")
        {                                                                                                                                                                                                                                                                                                                                                                                                                            
            alert("The server data matched"); 
        }
        else if($.trim(response)=="Inavailable") {                                                                                                                                                                                                                                                                                                                                                                               
            alert("The server data did not match"); 
        }
    }
});
于 2013-10-08T08:31:52.060 回答
-1

你的: response=="Available" 应该是这样的:

response.responseText == "Available"
于 2013-10-08T07:22:09.413 回答