我正在使用 Ajax file upload函数及其 javascript / jQuery library .
上传文件时,我不断收到此错误消息:SyntaxError: invalid label
这是我的 JS 脚本:
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;
}
我的 PHP 脚本有效(在使用 json/jquery 之前测试过),但我的 PHP 文件的 json 输出一定有问题。我尝试了两种方法。
我正在使用 json_encode格式化输出。这是我的一些 PHP 代码:
(...)
// 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);
更新
事实证明,我使用 Worpdress 的 _e()
来支持多语言。问题是 _e()
回显了内容,因此使 JSON 响应变得困惑。一旦我切换到 __()
它就起作用了。
感谢您帮助解决这个问题。
最佳答案
第一种方法不会生成有效的 JSON。查看 json_encode()
函数的输出,它可以正确生成它。主要问题是键和值没有用双引号括起来。
您是否尝试使用 Firebug 来确定错误的确切来源?每个 JSON 键都必须是一个字符串。在你的故障线路中显然不是这种情况。
关于php - json 数据返回无效标签错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672672/