views:

93

answers:

1

I need to evaluate the response of a Ajax.Request (using prototype) with a switch statement:

new Ajax.Request('http://localhost/somescript.php',{method:'post',parameters:params,onSuccess:
    function(response)
    {
        var result = response.responseText;
        switch (result)
        {
            case "ok":
            //do something...
            break;

            case "nok":
            //do something...
            break;

            case "almost":
            //do something...
            break;

            default:
            //do something...
        }
    }
    });

if I check the value of "result" sent by the server script the response is correct (a string: "ok", "nok" or "almost" depending on the parameters sent). But for some reason the switch always evaluates the default condition! I tried to concatenate "result" with an empty string before the switch statement, but no luck... What am I missing here?

A: 

Probably because the result contains a line break. Try trimming it with something like:

var result = response.responseText;
result = result.replace(/^[\s\r\n]+|[\s\r\n]+$/g, "");
Tomalak
thanks! worked!
JoaoPedro