Ajax Uncaught Syntax错误,无法识别的表达式:

0 javascript php mysql ajax jquery

我不知道为什么控制台会给我这个未被删除的语法...请帮帮我PLZ!

$(document).ready(function() {
  $("#idval").change(function() {

    var id = $(this).val();

    $.ajax({
      url: 'verif.php',
      type: 'GET',
      data: 'user=' + id,

      success: function(server_response) {
        var session = $(server_response).html();

        if (id == session) {
          console.log($("#" + d));
        } else {
          console.log("You shall not pass!");
        }

      },
      error: function(server_response, statut, error) {
        console.log("Can't be linked !");
      }
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

当用户输入时id,服务器检查id数据库是否在数据库中.如果是,服务器返回id控制台,如果不是,服务器应返回"字符串",但它返回未捕获....

T.J*_*der 5

这一行:

var session = $(server_response).html();
Run Code Online (Sandbox Code Playgroud)

没有意义.如果服务器成功回复ID,则直接使用server_response.

  success: function(server_response) {
    if (id == server_response) { // <== Here
      console.log($("#" + id));  // <== Also fixed apparent typo on this line,
                                 //     but that's not the reason for the
                                 //     error you're getting
    } else {
      console.log("You shall not pass!");
    }
  },
Run Code Online (Sandbox Code Playgroud)

$(server_response)要求jQuery使用server_responseHTML作为内置DOM元素或作为CSS选择器使用."无法识别的表达式"表明它看起来不像HTML,因此jQuery尝试将其用作选择器,但它不是有效的选择器.


在一个应该是评论的答案中,你说你已经将代码更新到(大部分)以上但是它仍然无效,你已经向我们展示了这个PHP代码:

while ($idval = $reponse->fetch()) {
    if ($idval){
        echo $idval['user'];
    }
    else{
        echo "nope";
    }
}
Run Code Online (Sandbox Code Playgroud)

如果if (id == server_response)不起作用,那就告诉我们这id不是完全匹配的server_response.使用PHP脚本的一个常见原因是,您无意中在输出响应的代码之前或之后包含空格,通常是在某处,通常是最后的换行符.

我们可以通过server_response.trim()使用jQuery的$.trimvia 来修改现代浏览器或支持旧版浏览器$.trim(server_response):

  success: function(server_response) {
    if (id == $.trim(server_response)) { // <== Here
      console.log($("#" + id));
    } else {
      console.log("You shall not pass!");
    }
  },
Run Code Online (Sandbox Code Playgroud)