简体   繁体   English

Ajax调用给出响应,但是成功,完成和错误功能未触发

[英]Ajax call gives response, but success,complete and error functions not firing

I'm trying to get sql results from a database through ajax call and display in another php page. 我试图通过ajax调用从数据库中获取sql结果,并显示在另一个php页面中。 My ajax call is: 我的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")
    });
}

} }

My json response has following format: 我的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" },...

This function returns a valid json response (as seen in Chrome DevTools). 此函数返回有效的json响应(如Chrome DevTools中所示)。 But none of the alert() is executed. 但是没有任何alert()执行。 But every time console shows successThis is a question . 但是每次控制台显示成功时,这successThis is a question I've scanned through several questions but none seems to resolve this particular issue. 我已经浏览了几个问题,但似乎都无法解决这个特定问题。 I want to display all the data in that json. 我想显示该json中的所有数据。 Any advice is greatly appreciated. 任何意见是极大的赞赏。

That is NOT how you code callbacks 那不是你编写回调的方式

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

needs to be 需要是

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

Try this, add function(){} 试试看,添加function(){}

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

instead of 代替

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

callback should be a function.so you need to call a function in callback complete and error or use anonymous function like you did in success callback. 回调应该是一个函数,因此您需要在完成回调和出错时调用函数,或者像成功回调一样使用匿名函数。

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

api Doc: https://api.jquery.com/jQuery.ajax/ api文件: https//api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM