This is driving me mad.
I have done plenty of ajax calls but this one I can’t get working.
(jquery/1.9.1)
$("#myfile").change(function(){
var $regform = $("#imgform");
var iData = $regform.serialize();
$.ajax({
url: 'upload-pic.php',
type: 'POST',
data: iData,
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal);
percent.html(percentVal);
},
success: function() {
var percentVal = '100%';
bar.width(percentVal);
percent.html(percentVal);
},
complete: function(data) {
$('imgstatus').html(data.msg);
}
})
});
The following php contains the test Json;
<?php
$result = array("msg"=>"Image Uploaded");
echo json_encode($result);
exit();
?>
The complete: function() shows the data as undefined and if I parse the data using: var obj = JSON.parse(data);
It throws this error, unexpected character at line 1 column 2 of the JSON data.
Don’t understand why.