5

How do I use FormData in wordpress ajax ? I am appending files and strings to formdata.

    var formdata = new FormData();
    formdata.append('name', 'This is Name')

    $.ajax({
        url: 'admin-ajax.php',
        type: 'POST',
        data: {'action':'plugin_save', 'data':formdata},
        contentType:false,
        processData:false,
        success: success,
        error: error
    });

   // php
   sends 0 ?
2
  • you're getting 0 back as the response from php? what have you done to determine it is related to your use of FormData? can we see the rest of your ajax code?
    – Milo
    Dec 4, 2014 at 16:04
  • If i pass a plain js object it works
    – NestedWeb
    Dec 4, 2014 at 16:50

1 Answer 1

10

The action should be part of the data object:

var formdata = new FormData();
formdata.append('name', 'This is Name');
formdata.append('action', 'plugin_save');

$.ajax({
    url: 'admin-ajax.php',
    type: 'POST',
    data: formdata,
    contentType:false,
    processData:false,
    success: success,
    error: error
});
1
  • I have been searching for so long. This is it. Thank you.
    – tehlivi
    Aug 17, 2018 at 17:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.