【问题标题】:Ajax call gives response, but success,complete and error functions not firingAjax 调用给出响应,但未触发成功、完成和错误函数
【发布时间】:2014-02-28 02:39:40
【问题描述】:

我正在尝试通过 ajax 调用从数据库中获取 sql 结果并显示在另一个 php 页面中。我的 ajax 调用是:

function newfunc(){
start += 10;
var params = parseURLParams(document.URL);
var datastring = "nextStart="+start+"&subID="+params["hidSubjectID"]+"&topic="+params["hidTopic"];
if(datastring!=''){
$.ajax({
        type:"POST",
        url:"ajax/getTopicQuestions.php",
        data: datastring,
        dataType: "json",
        success: function(json){ console.log("success"+json[0].question); alert("success");},
        complete: alert("complete"),
        error: alert("error")
    });
}

}

我的 json 响应格式如下:

[ { "qno": "3", "qbit": "(i)", "qinstruction": "", "question": "This is a question", "mark": "5", "examname": "B.Tech. Sixth Semester Examination", "examyear": "2011", "questionid": "368", "examtype": "BPUT Univ Exam", "topic": "HTML" },...

此函数返回一个有效的 json 响应(如 Chrome DevTools 中所示)。但是没有一个alert() 被执行。但每次控制台显示successThis is a question。我已经浏览了几个问题,但似乎没有一个可以解决这个特定问题。我想显示该json中的所有数据。非常感谢任何建议。

【问题讨论】:

    标签: javascript jquery ajax json


    【解决方案1】:

    这不是你编写回调的方式

       complete: alert("complete"),
        error: alert("error")
    

    需要

       complete: function() { alert("complete") },
        error: function() { alert("error") }
    

    【讨论】:

      【解决方案2】:

      试试这个,加function(){}

       complete: function(){ alert("complete");},
       error: function(){ alert("error");}
      

      而不是

       complete: alert("complete"),
        error: alert("error")
      

      【讨论】:

        【解决方案3】:

        回调应该是一个函数。所以你需要在回调完成和错误中调用一个函数,或者像你在成功回调中那样使用匿名函数。

         complete: function(){ alert("complete");},
         error: function(){ alert("error");}
        

        api文档:https://api.jquery.com/jQuery.ajax/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-03
          • 2018-08-05
          • 2011-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-25
          相关资源
          最近更新 更多