Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

My web page sends an AJAX call to my server and retrieves the value "clear".

However, the IF statement here says that "clear" != "clear".

function getData(){
				
				$.ajax({
					
					url: "getData",
					
					success: function(data){
						
						if(data=="clear"){
						
							$("#box").html("");
							
						}else{
						
							$("#box").append(data);
						
						}
						
						setTimeout(getData, 50);
					
					}
				
				});


I can tell because "clear" gets put into the box, instead of the box being cleared.

How can I fix this?
Posted
Updated 8-Apr-15 12:08pm
v3
Comments
Sergey Alexandrovich Kryukov 8-Apr-15 18:55pm    
There is also === :-)
No, it really means that one of your "clear" is not exactly "clear"...
—SA
Afzaal Ahmad Zeeshan 8-Apr-15 23:08pm    
The problem would be either HTML attributes, elements, encoding bla bla bla. See Solution 1 where it is clear that object should be equal. He also has mentioned in the comments, where type checking is also used (=== checks type equality also). Thus, this shows that your data is not equal to a simple "clear" value. It might be inside <p> or some other problem. See solution 2 for that.

Not true. Write this, and you will see the dialog box:
JavaScript
if ("clear" == "clear")
    alert("Of course it works!");


You can be sure: as I say your data is not exactly "clear". There is no such thing as miracle. A difference can be in some small detail. If you create and example when data is assigned to something, I will be able to tell you what's wrong.

[EDIT]

It's very important to understand how == and === operators really work:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Equality,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity.

See also comments to this answer below.

—SA
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 8-Apr-15 23:09pm    
+5, also you should include that type checking operator (===) in this answer. :)
Sergey Alexandrovich Kryukov 9-Apr-15 0:53am    
Thank you, Afzaal.
In this case, === won't really help, but there are recommendation to prefer === over == whenever possible, but I prefer to say that even better recommendation is to understand well how those operators work, clearly understand that underfined, null, 0, etc are all different objects, and so on. But I'll follow your advice and will add links on the operators. == is less trivial than pretty obvious ===.
—SA
Afzaal Ahmad Zeeshan 9-Apr-15 5:34am    
I would like to agree to your claim, "=== won't really help". Because the problem is that the value of data variable is not equal to "clear" if it were, it would have to been resolved as true. As I have already mentioned in my comment (and you have mentioned it also in your solution), the problem is, that the response is being generated as HTML element (e.g. <p>clear</p>), then he should use a responsive literal to check against. JavaScript is such a language in which the base type is somewhat a String, when ever you are using something it is like a String, that is why 2 + '2' resolves in 22 where as 2 - '2' resolves in 0 (casting the preceding '2' into an integral value). That is why, type checking should also be considered, but above all that, we should understand that the response would be sent back as HTML... Not just a simple text inside the variable.
Sergey Alexandrovich Kryukov 9-Apr-15 10:36am    
I understand, but let's see, == works via calling *.toString() on both operands, but it happens only if === returns false. (Explained in article on == I referenced after your advice.) That is, if === is false, == still can return true, but if == returns false, === certainly returns false. The inquirers claims that == returns false, so === will return false anyway. By the way, the inquirer sends ASCII-encoded bytes on the server side, please see the comment below.

Isn't that logical?

—SA
Sergey Alexandrovich Kryukov 9-Apr-15 10:44am    
HTML could be the reason. Note that === won't help by the reason I explained, because typeof("clear") will return string, but, say, typeof("<p>clear</p>") will also return 'string'. So the values are different. Then, the inquirer won't see the difference because this HTML is inserted in HTML and still read "clear". Conclusion: the inquirer can debug it by calling alert('[' + code +']');

The whole idea of string comparison was wrong in first place. Please see Solution 4.
Thank you for the idea; I credited you for it in that post.

—SA
Debug in your browser to find out what is actually inside data.
Here is a little about debugging Javascript in Chrome - Debugging JavaScript[^].
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 8-Apr-15 23:09pm    
+5
Abhinav S 9-Apr-15 0:14am    
Thanks.
Debug is by calling
JavaScript
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
JavaScript
$("#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
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 9-Apr-15 11:12am    
+5, this was exactly what I talking about. The actual problem is not the comparison. It is a confusion that was raised because the response is not a string value only, it is an HTML markup, even the simplest word would be wrapped in. :)

Thank you very much for the credit you gave me sir. :) I really do appreciate it.
Sergey Alexandrovich Kryukov 9-Apr-15 11:28am    
Thank you Afzaal, but it was you who brought me to the idea.

The actual problem is not the comparison itself, but the whole idea of using string comparison, more exactly, using strings representing data instead of data. It may seem weird statement because between ASP.NET and JavaScript everything is passed as data anyway, but still, strings should not be used directly. Explicit serialization/deserialization handles strings only internally, so you use 1) reliable well debugged code, in this case, JSON; 2) you abstract from detail of data presentation, in this case, HTML.

—SA
Afzaal Ahmad Zeeshan 9-Apr-15 11:41am    
Correct, that is exactly why JSON is used for API calls as a major representation when actual data is returned for a request. Developer can then use is as objects and get values; which would be real string values and == operator would return true if they have equal values.
Sergey Alexandrovich Kryukov 9-Apr-15 11:53am    
Sure. And it could be not string. If you look at the use of "clear" in "if", you will see that the semantic of it is exactly as the use of true or false value. True of false objects would be serialized and deserialized correctly without any problems, so the JavaScript object would have real Boolean object, true or false.
—SA
Changed

bytes = System.Text.Encoding.Unicode.GetBytes("clear")


to

bytes = System.Text.Encoding.ASCII.GetBytes("clear")
 
Share this answer
 
Comments
Member 11450536 13-Apr-15 15:55pm    
Why did this deserve a 1-star rating?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900