-1

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"}
Khazl
  • 125
  • 2
  • 9
  • 2
    What's an example value of `text`? Exactly which `if` are you referring to? – p.campbell Aug 04 '11 at 15:46
  • It is not likely that you've found a bug in one of the most basic pieces of core JS functionality after years of it having been in use. That said, we can't help you figure it out if we have no idea what the data you're working with looks like, which `if` you're "falling into", etc. – JAAulde Aug 04 '11 at 15:46
  • Why do you need the for loop if you're only interested in meinjson.text? Could you post an example of the text you're dealing with? – alnorth29 Aug 04 '11 at 15:47
  • 1
    You have a straightforward debugging problem, you have three `if` statements but refer to "the if", you don't give the test data, you have a `for` loop with only one meaningful iteration, and you don't describe any attempts you have made to save the problem yourself. Voting to close – Michael Lorton Aug 04 '11 at 15:49
  • {"command":"SENDTEXT","text":"Lorem+Ipsum+dolor%2C+test","specialcommand":"CONNECTACCEPT"} – Khazl Aug 04 '11 at 15:50
  • It is possible that text appears several times, so the loop. And i mean the last if "if (text2 == undefined || ..." – Khazl Aug 04 '11 at 15:51
  • 1
    text may appear multiple times in your rendering of the JSON, but it will only appear once after it's been converted to a JS object. The same is true of command. If you're representing a series of commands you should create an object for each and pass them as an array. – alnorth29 Aug 05 '11 at 06:24

2 Answers2

1

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":.

alnorth29
  • 3,525
  • 2
  • 34
  • 50
0

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");
}
Community
  • 1
  • 1
p.campbell
  • 98,673
  • 67
  • 256
  • 322