开发者

Switch statement always evaluates the default condition

开发者 https://www.devze.com 2022-12-16 04:45 出处:网络
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:

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?


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, "");
0

精彩评论

暂无评论...
验证码 换一张
取 消