jquery file upload json response is not getting

Viewed 83
     var uploadformurl = '/upload.php';
     var form_data = new FormData();
     form_data.append("upload", $("#upload").prop("files")[0]);
     $.ajax({
       url: uploadformUrl,
       dataType: 'script',
       cache: false,
       contentType: false,
       processData: false,
      data: form_data,                         
      type: 'post',
     complete: function(data){
        console.log(data);
    }
   });

I am uploading one file through jquery ajax. Upload function is working fine. But i am not getting any response from Api.

eg:

{"error":"","success":"success"}

How i can catch the api response. Any help will be appreciated.

1 Answers
var uploadformurl = '/upload.php';
 var form_data = new FormData();
 form_data.append("upload", $("#upload").prop("files")[0]);
 $.ajax({
   url: uploadformUrl,
   dataType: 'script',
   cache: false,
   contentType: false,
   processData: false,
  data: form_data,                         
  type: 'post',
 success: function(data) {
        // Run the code here that needs
        //    to access the data returned
        console.log(data)  
        return data;
    },
    error: function() {
        alert('Error occured');
    }
});

try something like this, I believe the issue is AJAX cant return a value from its asynchronous calls so you have to pass it thru a callback function like success().

Heres a image of how a ajax call flows

Related