4

I could not do new FormData or insert a json sequence

my Json data:

var data = {
        status: "COMPLETE",
        personName: $('#full_name').val(),
        personPhone: $('#phone_number').cleanVal(),
        personEmail: $('#e_mail').val(),
        referanceFullname: $('#person_full_name').val(),
        referancePhone: $('#person_phone_number').cleanVal(),
        referanceEmail: $('#person_e_mail').val()
      };

How do I attach this to new FormData?

3
  • Why do you need FormData? Commented Jul 31, 2017 at 10:32
  • xyproblem.info Commented Jul 31, 2017 at 10:34
  • @hurricane my form have 2 disable input. I try serializeArray but not take disable inputs value Commented Jul 31, 2017 at 10:51

3 Answers 3

7

Use append to add new data. Checkout MDN about formData append

var fd = new FormData();

var data = {
        status: "COMPLETE",
        personName: $('#full_name').val(),
        personPhone: $('#phone_number').cleanVal(),
        personEmail: $('#e_mail').val(),
        referanceFullname: $('#person_full_name').val(),
        referancePhone: $('#person_phone_number').cleanVal(),
        referanceEmail: $('#person_e_mail').val()
};


$.each(data, function(key, value){
  fd.append(key, value);
})
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this code is better. It worked like I wanted. Thx
Okay if there nested object ? data ={ first_name: 'Tom'} ?
0

Found one: https://github.com/therealparmesh/object-to-formdata/

object-to-formdata is a convenient JavaScript function that converts an object to a FormData instance.

1 Comment

But this one is for Typescript. Is there any plugin for javascript?
-1

You can do by this code.

 var data = new FormData();
 data.append("status", "COMPLETE");
 data.append("personName", $('#full_name').val());
 data.append("personPhone", $('#phone_number').cleanVal());
 data.append("personEmail", $('#e_mail').val());
 data.append("referanceFullname", $('#person_full_name').val());
 data.append("referancePhone", $('#person_phone_number').cleanVal());
 data.append("referanceEmail", $('#person_e_mail').val());

 $.ajax({
            type: "POST",
            url: url,
            processData: true,
            data: data,
            beforeSend: function (xhr) {                    
            },
            success: function (data) {                    
            },
});

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.