问题描述:

The file uploads through ajax no problem, but when a json encoded array is returned to ajax I get this error when trying to parse it:

Uncaught SyntaxError: Unexpected end of input

Client Side Script

jQuery(".target-input").change(function() {

var formData = new FormData();

formData.append("file", this.files[0]);

jQuery.ajax({

url: "../wp-content/plugins/site_bannermanager/site_ajax_server_add.php",

file: formData

}).done(function(response) {

console.log(JSON.parse(response));

});

});

site_ajax_server_add.php

foreach ($_FILES as $key => $data) {

exit(json_encode(wp_handle_upload($data)));

}

网友答案:
jQuery(".target-input").change(function () {
  var formData = new FormData();
  formData.append("file", this.files[0]);
  jQuery.ajax({
    url: "../wp-content/plugins/site_bannermanager/site_ajax_server_add.php",
    file: formData,
    success: function(response) {
      console.log(JSON.parse(response));
    }
  });
});

Should do the trick. Looks like your forgot some of your closing brackets. I also made the success function a bit better (but you can change it back if you want, I just think it makes it more readable.

相关阅读:
Top