I'm using Ajax file upload function with its javascript / jQuery library.
When uploading a file, I keep getting this error message: SyntaxError: invalid label
This is my JS script:
jQuery('.uploadImage').live('click',function() {
ajaxFileUpload();
});
(...)
function ajaxFileUpload(){
jQuery.ajaxFileUpload({
url:'../wp-content/plugins/wp-filebrowser/uploader.php',
secureuri:false,
fileElementId:'uploadFile',
dataType: 'json',
success: function (data, status){
if(typeof(data.error) != 'undefined'){
if(data.error != ''){
alert(data.error);
}else{
alert(data.msg);
}
}
},
error: function (data, status, e){
alert(data + ' - ' + status + ' - ' + e);
}
}
)
return false;
}
My PHP script works (tested before using json / jquery), but there must be something wrong with my json output from my PHP file. I've tried two approaches.
I'm using json_encode to format the output. This is some of my PHP code:
(...)
// Error message is at this stage empty.
move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile);
$respons = $_FILES["file"]["name"]._e(' successfully uploaded');
$data = array( "error"=> $error, "msg"=> $respons );
echo json_encode($data);
UPDATE
It turns out that I was using Worpdress's _e()
for supporting multilanguage. The problem is that _e()
echo's the content and therefor clutters the JSON response. Once I switched to __()
it worked.
Thanks for helping medebug this poblem guys.