簡體   English   中英

Ajax調用給出響應,但是成功,完成和錯誤功能未觸發

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

我試圖通過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中的所有數據。 任何意見是極大的贊賞。

那不是你編寫回調的方式

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

需要是

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

試試看,添加function(){}

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

代替

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

回調應該是一個函數,因此您需要在完成回調和出錯時調用函數,或者像成功回調一樣使用匿名函數。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM