代码之家  ›  专栏  ›  技术社区  ›  ekka

AJAX——请求正确完成,但执行了错误函数

  •  0
  • ekka  · 技术社区  · 7 年前

    我有一个来自服务器的AJAX请求,它在数据库中保存了一个用户:

    JavaScript

    u.first_name = "chaabaoui";
    u.last_name = "aymene";
    u.mot_de_passe = "123";
    u.type = 0;
    u.email = "ekka@gmail.com";         
    $.ajax({
        url : "http://localhost:8080/utilisateurs/addUser",
        headers: { 
            'Accept': 'application/json',
            'Content-Type': 'application/json' 
        },
        type : "POST",
        data : JSON.stringify(u),
        dataType : "application/json",
        success : function(response) {
                      console.log("the request is done,");
                      if(response == "true") {
                          console.log("and the user was saved");
                      } else {
                          console.log("but the user was not saved");
                      }
                  },
        error : function(error) {
                    console.log("the request was failed ", error);
                },
        complete : function(xhr , status) {
                       console.log("the requeste is complete");
                   }
    });
    

    问题用户被保存到数据库中,但会显示来自错误功能的错误消息(“请求失败”),而不是成功消息。我需要知道这里出了什么问题。最重要的是,为什么不执行成功消息?

    提前谢谢你。

    1 回复  |  直到 7 年前
        1
  •  0
  •   german1311    7 年前

    关闭控制台应该很难看到错误,因为您有:

     error : function(error){
                 console.log("the request was failed ", error);
             },
    

    但如果它没有显示任何内容,只需使用chrome(或其他浏览器)在网络选项卡(开发人员工具)上检查您的请求,它至少应该是一个带有响应消息的错误请求

    https://developers.google.com/web/tools/chrome-devtools/network-performance/reference