简体   繁体   中英

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. My ajax call is:

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:

[ { "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). But none of the alert() is executed. But every time console shows 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. 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(){}

 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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