Debug is by calling
alert( '[' + data + ']' );
The brackets are needed to see where the result of
data.toString()
is started and finished (please read the explanation on == operation, same
toString
thing is used in
alert
).
Chances are, you can see some markup around "clear", instead of "clear" (credit to Afzaal, please see his comment to Solution 1). Why you did not see this markup? Because in
$("#box").append(data);
you see not the string
data
, but HTML rendering of it.
Even in HTML you could observe the problem if you append not
data
, but, say
"<pre>" + data + "</pre>"
. You can try it, too.
From the very beginning, the whole idea of comparison looked wrong, not the result. You need to work with data, avoiding working with string representing data. I bet your "clear" is supposed to play the role of Boolean flag meaning "clear". Then pass it, not hard-coded string! How? Easy, exchange, for example, JSON data. If you deserialize JSON into JavaScript objects, member of this objects will be of different types, including string and Boolean objects of something you can safely compare with "if".
—SA