4

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 ?
  • 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 '14 at 16:04
  • If i pass a plain js object it works – NestedWeb Dec 4 '14 at 16:50
8

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
});
  • I have been searching for so long. This is it. Thank you. – tehlivi Aug 17 '18 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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