我已经在Plupload之上设置了一些PHP安全检查,并通过使用与Plupload相同的JSON-RPC表示法来生成错误消息,以保持一致。尽管无可否认,我不太熟悉这个过程。
我在服务器端生成的错误消息实例如下所示:
$err_msg = '{"jsonrpc" : "2.0", "error" : {"code": 204, "message": "File type not supported. "}, "id" : "id"}';
在接收PHP脚本时,Plupload脚本配置如下:
$('#uploader2').plupload('getUploader').bind('FileUploaded', function (uploader, file, info) {
//alt 1 = THIS WORKS
var err_msg = JSON.parse('{"jsonrpc" : "2.0", "error" : {"code": 204, "message": "File type not supported. "}, "id" : "id"}');
alert(info.error.code); // prints 204
// alt 2 = GENERATES ERROR SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
var err_msg = JSON.parse(info);
// alt 3 = ASSUMING JQUERY IS ALREADY PARSING JSON
alert(info); // gives object
alert(info.error.code); // prints UNDEFINED
}
在alt 1
中,为了测试,我将字符串直接粘贴在脚本上;在这种情况下,它工作得很好,我可以按预期访问对象。
在alt 2
中,我假设我需要解析JSON,所以这就是导致语法错误的原因。在SO的其他地方,我发现这个错误表明jQuery已经在解析JSON了,所以不应该再这样做了。
在alt 3,
中,我尝试像通常那样访问对象,但得到undefined
。
我在这里错过了什么?
按照建议添加console.log:
"{"jsonrpc" : "2.0", "error" : {"code": 204, "message": "Ne correspond pas aux types de fichiers acceptés. "}, "id" : "id"}"
感谢
在服务器端将错误消息写成纯文本:
response.getWriter().write(message);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
在客户端,您将在中找到书面消息
Error: function(up, err) {
alert(err.response);
...