Based on your comment:
"Thanks for that suggestion. $.ajax does not load the at all. It shows an error which you may know of. Although your suspicion is right because, when I use "this.ajax" the file does get uploaded but the console shows an error "Type has no method ajax"
- You can't upload files using
$.ajax()
the same way you usually upload your form data. - Because of the error when you use
this.ajax
the form gets submitted. That's why you see the JSON response in the browser.
The question is where does this.ajax
come from? Did you copy the code from an example that uses a library you haven't included?
In order to upload files via AJAX you need some kind of plugin (doing it yourself is some effort, especially if you need support for older browsers).
Update
The submit handler should be as follows:
form.submit(function (e) {
e.preventDefault();
$(this).ajaxSubmit().done(function (data) {
var x = JSON.parse(data);
alert("Success : " + x);
}).fail(function (data) {
var x = JSON.parse(data);
alert("Error : " + x);
});
});
Explanation: Since you want to call a jQuery plugin you need a jQuery object, hence $(this)
. ajaxSubmit()
does not need any arguments (you can pass in options if you want, though). Because I didn't pass in arguments the callbacks are appended, in this case with done
and fail
(instead of success
and error
).