[英]Storing ajax jquery into a variable and acessing it with the variable
我可以從這樣的變量調用jquery ajax嗎?
var save = $.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxInsertCheck.php",
data: dataString,
cache: false,
success: function(response){
alert(response.a);
}
// call ajax jquery
save
我怎樣才能像該函數那樣調用該請求? 有人可以幫忙嗎?
您需要使用一個函數 。 函數封裝了我們將來要調用的行為。 您可以傳遞函數參數 ,這些參數指示您希望函數如何執行。
當我們想要在代碼中具有可重用位或為了代碼清晰起見,通常使用函數:
function save(){ // declare a function and call it save
return $.ajax({ // a return means we're letting people from the outside use it
type: "POST",
dataType: 'json',
url: "functions/ajaxInsertCheck.php",
data: dataString,
cache: false,
success: function(response){
alert(response.a);
}
});
}
然后,您可以從自己的代碼中調用它:
save(); // call the save function performing this action.
請注意,您的代碼中已經有很多功能示例,例如分別由瀏覽器和jQuery定義的alert
和$.ajax
。
如果您的意思是,您可以在編寫呼叫時推遲執行該調用,否。 該調用將立即執行。
在http://api.jquery.com/jquery.ajax/上查看文檔-特別是在“ jqXHR對象”標題下。 根據JQuery的版本,它是不同的,但是您將獲得一個實現Promise接口的對象。
但是,使用javascript功能,您可以創建一個函數:
function saveFunction() {
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxInsertCheck.php",
data: dataString,
cache: false,
success: function(response){
alert(response.a);
}
});
}
saveFunction();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.