jQuery Form submit file - getting messages returned by the page

I am using this code for submitting a file using jQuery:

$(document).ready(function() {
    $('form').submit( function() {
        $.ajax({
            type: 'post',
            url: 'http://localhost/test/upload.html',
            data:{file},
            success: function (data) {
                alert("done")
            }
        })
    })
})

Open in new window


The problem I am facing is that I am not getting the messages (either successful or error messages) given by the page.

How can I change this code so that it returns error or successful messages given by the upload.html page?
LVL 1
error2013Asked:
Who is Participating?
 
Julian HansenCommented:
Where is file defined?
File uploads should be POSTED
Your target script appears to be an .html file - how is that going to process an upload?

To upload a file with AJAX you use the FormData interface
How you do this depends on a variety of factors with respect to how you are obtaining the file but you can read more about that here

If you have an <input type="file"> element then you can create the FormData object with the form element as a parameter - you can then use the FormData object in your AJAX request.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects#Sending_files_using_a_FormData_object
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.