开发者

Javascript Split (Bug?) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_StackOverflow中文版 Closed 11 years ago.

I try to split a string at the colon and to check whether it was successful. The api.php gives me back a JSON.

$.ajax({
    type: "POST",
    url: "api.php",
    data: { "somedata": "123"},
    success: function (data, status, xhr) {
        if (data.indexOf("text") != -1) {
            var meinjson = $.parseJSON(data);
            for (var key in meinjson) {
                if (meinjson.hasOwnProperty(key) && key=="text") {
                    text = meinjson[key];
                    text = text.replace(/\+/g, " ");
                    text = decodeURIComponent(text);
                    if (text.indexOf(":") !== -1) {
                        text = text.split(/:(.+)?/);
                        var text1 = text[0];
                        var text2 = text[1];
                    }
                    if (text2 == undefined || text1 == undefined || text1 == void 0 || text2 == void 0 || text1=="" || text2=="") {
                        alert("fail");
                    }
                }
            }
        }
    }
});

I can not explain why the internet explorer to always fall into the last if but does not firefox and chrome. A example of data is:

{"command":"SENDTEXT","text":"Lorem+Ipsum","command":"SENDTEXT","text":"Lorem+Ipsum+dolor","specialcommand":"CONNECTACCEPT"}


You're missing a }. If you indent after the first if in the success function you'll see where.

Update:

Your are splitting on a comma, but the values of "text" in your data do not contain commas. Where it says "text":"Lorem+Ipsum" the actual value is "Lorem+Ipsum", it will not include "text":.


Suggest refactoring your statement to leverage falsely checks. Undefined and void 0 are essentially the same. Along with empty string, they are all falsey values.

if (!text2 || !text1 )
{
    alert("fail");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜