weixin_33749242 2017-02-17 13:49 采纳率: 0%
浏览 18

jQuery Ajax参数问题

This is my code:

$('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 );
            });
        });

My problem is that this var postDataUpdate it didn't passed to ajax. On firebug the contact doesn't appear. If I do console.log(postDataUpdate) before my ajax request i got my array .

So any idea about this ?

  • 写回答

1条回答 默认 最新

  • weixin_33736048 2017-02-17 14:08
    关注

    postDataUpdate should be an object, instead of an array:

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

    Check this snippet:

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

    </div>
    
    评论

报告相同问题?