BernardA
BernardA

Reputation: 1523

Plupload Is Not Able to Access Error Message For JSON Object

I have set up some PHP security checks on top of Plupload and generating error messages as it goes by using the same JSON-RPC notation as Plupload to be consistent. Though admittedly I am not quite familiar with this process.

An error message instance that I'm generating on the server side looks like this:

$err_msg = '{"jsonrpc" : "2.0", "error" : {"code": 204, "message": "File type not supported. "}, "id" : "id"}';

On the receiving PHP script, the Plupload script is configured as follows:

            $('#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 
        }

In alt 1, just to test, I pasted the string directly on the script; in that case it works fine and I can access the object as expected.

In alt 2, I am assuming that I need to parse the JSON and so this is what's giving me the syntax error. Looking elsewhere on SO, I found that that error indicates that jQuery is already parsing the JSON, so it should not be done again.

In alt 3, I try to access the object as one normally would but get undefined.

What am I missing here?

Adding console.log as suggested:

"{"jsonrpc" : "2.0", "error" : {"code": 204, "message": "Ne correspond pas aux types de fichiers acceptés. "}, "id" : "id"}"

Thanks

Upvotes: 0

Views: 191

Answers (1)

Alejo Ferraria
Alejo Ferraria

Reputation: 11

In server side write the error message as plain text:

response.getWriter().write(message);  
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

In client side you will find the written message in:

Error: function(up, err) {
alert(err.response);    
...

Upvotes: 1

Related Questions