gpt4 book ai didi

javascript - jQuery ajax 参数问题

转载 作者:行者123 更新时间:2023-11-30 15:30:37 26 4
gpt4 key购买 nike

这是我的代码:

$('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 没有传递给 ajax。在 firebug 上,contact 不会出现。如果我在我的 ajax 请求之前执行 console.log(postDataUpdate) 我得到了我的数组。

那么对此有什么想法吗?

最佳答案

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();
}
});
[..]

检查这个片段:

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);

关于javascript - jQuery ajax 参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com