0

I have some jquery ajax call that returns a simple string. The interpretation of the return string is working in Firefox and IE (class is set to ok), but not in Chrome (class is set to wrong)?

The php in myurl simply returns a string with a numerical value. In Chrome, an alert(type of result.responseText); also gives me a string and debugging suggest that the value is indeed 100. But Chrome for some reason does not dive into the if statement.

Any ideas?

 $.ajax({
  url : 'myurl',
  data:{o:1},
  dataType:"text",
  complete : function (result) {
  if (result.responseText==100){
   $("#mydiv").addClass("ok");
  }
  else {
   $("#mydiv").addClass("wrong");
  }
 }
}); 
mmzc
  • 602
  • 8
  • 19
  • 2
    Why not avoid the reliance on implicit conversions and just use `if (result.responseText === "100")` ? – Alexander Pavlov May 11 '12 at 09:56
  • Alexander is right.This should do – techie_28 May 11 '12 at 09:59
  • Thanks, this put me on the right track. It appeared that myurl returned a string with an additional BOM ("100" was returned, not "100"). Setting the documument encoding in UTF-without DOM fixed it. This helped: http://stackoverflow.com/questions/6538203/how-to-avoid-echoing-character-65279-in-php-this-question-also-relates-to-java – mmzc May 14 '12 at 10:04

5 Answers5

1

try the condition as if (result.responseText== "100"){

Akash Yadav
  • 2,411
  • 20
  • 32
0
complete : function (result) {

result is already responseText when you use jQuery. If you absolutely want to use the responseText property, you can use the following:

complete : function(result, statusText, xhrObject) {
    // access xhrObject.responseText there
}
Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
0

Do you try to compare response with "100"?

if (result.responseText=="100"){
Boguslaw
  • 41
  • 2
0

try string comparison like if (result.responseText== "100")

Or convert the response into an integer and then compare like this

if (parseInt(result.responseText,10) == 100)

if you are send back only a number. :)

Prasenjit Kumar Nag
  • 13,391
  • 3
  • 45
  • 57
0

It appeared that myurl returned a string with an additional BOM ("100" was returned, not "100"). Setting the documument encoding in UTF-without DOM fixed it. This helped: How to avoid echoing character 65279 in php? (This question also relates to Javascript xmlhttp.responseText (ajax))

Community
  • 1
  • 1
mmzc
  • 602
  • 8
  • 19