簡體   English   中英

jQuery:在調用ajax的函數內部返回false

[英]jQuery: return false inside of a function that calls ajax

對於這個特定的問題,我正在使用Tag-it https://github.com/aehlke/tag-it ,這是一個專用於使用標簽的jQuery插件

我有一個標簽列表,我正在使用ajax使用jQuery ui自動完成填充

我需要做的是,當ajax調用返回false時,向特定函數BeforeTagAdded返回false,這實質上是說數據庫拒絕了此標記條目,而不在客戶端瀏覽器中顯示該標記

開發人員指出:“為澄清起見,只需在回調中返回false即可拒絕該標簽。” 那就是我想要完成的

除了以下代碼,我還嘗試了什么:

  1. 將結果全球化為變量,然后將ajax放入結果中,只是返回未定義
  2. 使用return false和/或event.preventDefault()+ stopPropagation()的組合
  3. 使用ajax調用的完成或完整方法

Ajax調用返回的唯一結果是result:true或result:false,與此同時,除以下代碼外,我還將處理一個jsfiddle

 beforeTagAdded: function(event, ui) {              
     if ( !ui.duringInitialization){ //do not fire on page initialization

            $.ajax({
                    url: "handlers/tags.ashx",
                    dataType: "json",
                    data: {
                        idnumber: entity_id,
                        tag: ui.tagLabel,
                        operation:"tag"
                    }
                }).done(function(data){
                    if(data.result == false){
                      event.preventDefault();
                      event.stopPropagation();
                    }
                }).complete(function(data){

                });

         }
 },

Ajax是異步的。 complete / done / success /等是事件處理程序。 在表示收到HTTP響應的事件到達之前,它們不會觸發。

您不能從這些事件處理程序內部返回,也不能從beforeTagAdded函數修改事件,因為beforeTagAdded將在收到HTTP響應之前完成並返回。

您需要重新考慮您的方法。 這可能涉及始終取消beforeTagAdded事件,但是要從Ajax事件處理程序內部以編程方式重新啟動它。


 beforeTagAdded: function(event, ui) {              
     if ( !ui.duringInitialization){ //do not fire on page initialization
            event.preventDefault();
            event.stopPropagation();
            $.ajax({
                    url: "handlers/tags.ashx",
                    dataType: "json",
                    data: {
                        idnumber: entity_id,
                        tag: ui.tagLabel,
                        operation:"tag"
                    }
                }).done(function(data){
                    if(data.result != false){
                         something(); // that would have happened if you hadn't called preventDefault before
                    }
                }).complete(function(data){

                });

         }

},

為什么不只是將AJAX與beforeTagAdded函數分開,而將beforeTagAdded用作回調?

像這樣:

beforeTagAdded : function (event, ui, data) {
  if (!ui.duringInitialization) {
    // do something with the data
  }
},
initAjax : function(event, ui) {
  // make sure entity_id is defined
  var entity_id = '';

  $.ajax({
    url: "handlers/tags.ashx",
    dataType: "json",
    data: {
      idnumber: entity_id,
      tag: ui.tagLabel,
      operation:"tag"
    }
  }).complete(function (data) {
    beforeTagAdded(event, ui, data);
  });
}

然后,無論您在代碼中的beforeTagAdded()位置調用beforeTagAdded()將其切換為initAjax() 現在,您可以在beforeTagAdded內部擁有ajax數據,以執行所需的任何操作。

暫無
暫無

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

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