2017-02-17 12 views
1

これは私のコードです:jQueryのAJAXのparams問題

$('body').on('click', '.update_contact_profile', function(){ 
      var url = $("#ajaxUrl").val(); 
      var ids = $(this).closest("div").nextAll(".contact-extra-info").find(".contact-ids").attr("id"); 
      ids = ids.split("-") 
      var contactId = ids[0]; 
      var customerId = ids[1]; 
      var postDataUpdate = []; 
      $(this).closest("div").nextAll(".update_elements").find(".value :input").each(function(i, itemVal){ 
       if ($(this).val()) { 
        postDataUpdate[''+$(this).attr('id')+''] = $(this).val(); 
       } 
      }); 
      var request = $.ajax({ 
       url: url, 
       method: "POST", 
       data: { 
        id   : contactId, 
        contact : postDataUpdate, 
        form_key : FORM_KEY, 
        customerId : customerId 
       } 
      }); 

      request.success(function(text) { // replace the ajax_wrapper with the new one 
       $(".ajax_wrapper").replaceWith(text); 
       $("#contact_form").find('select').selectize(); 
      }); 
      request.fail(function(jqXHR, textStatus) { 
       alert("Request failed: " + textStatus); 
      }); 
     }); 

私の問題は、このVAR postDataUpdateは、それがアヤックスに渡されていなかったということです。火かき棒の上にcontactは表示されません。私は私のアヤックスの要求の前にconsole.log(postDataUpdate)をすれば私は私の配列を得た。

これについてのご意見はありますか?

[..] 
var postDataUpdate = {}; 
$(this).closest("div").nextAll(".update_elements").find(".value :input").each(function(i, itemVal){ 
    if ($(this).val()) { 
     postDataUpdate[''+$(this).attr('id')+''] = $(this).val(); 
    } 
}); 
[..] 

チェックこのスニペット:

+0

VAR postDataUpdate = [])。それは余分な括弧ですか? – Paul

+0

'[]]' –

+0

'に ')'がありますが、 '[]'が間違ったコピー貼り付けでした。それは問題ではない、私は私のポストを更新しました。コンソールにはエラーメッセージが表示されませんでした。 – Chester

答えて

2

postDataUpdate は、配列の代わりに、オブジェクトであるべきである

var asArray = []; 
 
asArray[1] = "foo"; 
 
asArray["foo"] = "bar"; 
 
console.log("asArray:"); 
 
console.log(asArray); 
 

 
var asObject = {}; 
 
asObject[1] = "foo"; 
 
asObject["foo"] = "bar"; 
 
console.log("asObject:"); 
 
console.log(asObject);