JS API Breaks with both "success" and "error"


(Squiz) #1

Has anyone else noticed that the JS API sometimes returns both a success AND error message, invalidating the JSON and going nuts when the browser tries to eval() it?

    {"error":"Unable To Regenerate Metadata For Asset"}{"success":["Metadata field #93701 has been successfully set to \"xyz\" for Asset \"abc\" (#101535)"}


Does anyone know if this has been fixed in a later version? I can't find any reference to it.

(Anthony Barnes) #2

I'd pop this one in the bug tracker as it doesn't look intended. It may have completed 2 actions that gave a different result, but should be returning them inside the same JSON string.


(Squiz) #3

I’ll do that when I get a moment.


As a side note for anyone else who’s interested, I’ve created the following modification, although it won’t work in anything below IE 8 without an additional JSON parser.


    function jsonToObj(json)
    {
    // Make the conversion
    var jsonObj = false;
    try{
        jsonObj = JSON.parse(json);
    }catch(e){
        jsonObj = {error:'The server returned garbage where JSON was expected. ('+e+') '+json}
    }
    
    return jsonObj;
    
    }


This will catch invalid JSON as well as 404 pages and blank pages (for instance from PHP errors).

(Steve Jordan) #4

[quote]
I’ll do that when I get a moment.



As a side note for anyone else who’s interested, I’ve created the following modification, although it won’t work in anything below IE 8 without an additional JSON parser.


    function jsonToObj(json)
    {
    // Make the conversion
    var jsonObj = false;
    try{
        jsonObj = JSON.parse(json);
    }catch(e){
        jsonObj = {error:'The server returned garbage where JSON was expected. ('+e+') '+json}
    }
    
    return jsonObj;
    
    }


This will catch invalid JSON as well as 404 pages and blank pages (for instance from PHP errors).
[/quote]

Ash,

FWIW I'm having exactly this issue with a call to setMetadata(). The browser reports an error because invalid JSON is returned, but the metadata is properly set.

I'll try the overloading approach, thanks for the tip.

Steve