1

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 ?

6
  • var postDataUpdate = []); is that an extra bracket? Commented Feb 17, 2017 at 13:51
  • You have an ) in your []) Commented Feb 17, 2017 at 13:51
  • srry the []) was a wrong copy paste. Thats not the issue, I updated my post. In the console I got no error messages. Commented Feb 17, 2017 at 13:52
  • 1
    should be var postDataUpdate = {}; Commented Feb 17, 2017 at 13:56
  • @GeneR can u give me an example ? :) Commented Feb 17, 2017 at 14:01

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.