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.
The first approach does not produce valid JSON. Take a look at the output of the
json_encode()-function, which generates it correctly. The main problem is that the keys and values are not enclosed by double quotes.Did you try to use firebug, to identify the exact source of the error? Every JSON-key has to be a string. This is obviously not the case in your faulty line.