in chrome : conditional statement fails on jquery/ajax result

204 views Asked by At

this is my code . it sends a ajax request to a page and alerts the result

function num(){
    $.ajax({
        type: 'POST',
        url: 'num.php',
        success: function(data) {
            data = parseInt($.trim(data));
            if(data == 1 )
                alert(' its num 1');
            else  
                alert(data)
        }
    })
}

num.php

<?php echo 1; ?>

it works fine in ie and ff but in chrome this conditional statement doesn't work for some reason and i get '1' in the alert window

3

There are 3 answers

0
max On BEST ANSWER

in my personal experience your num.php file or one of the included files has a bad encoding and it's adding some invisible characters to the result . try changing encoding to utf8 without bom and remove included files from num.php

0
Piotr Kula On

Instead of alerts.. Use console.log(data) because it will actually show you the TYPE and you can log more detials isntead of using annoying alerts.

The console could show you potential type conversion (even though you are using parseInt) it could be cast to string again.. who knows.. because all these browsers use their own J/Engines... conventions could be slightly off.

eg

  • data = '1' (chrome)
  • date = 1 (ie/ff)

Also console.log($(object)) reproduces PHP's print_r(object)

Alert does not do that.. jsut say [Object object] - Try and steer away from alerts.

Use the Right Click - Inspect to access the Console in Chrome

0
cycaHuH On

For convert string you can try:

data = data/1;

For compare use '===':

if(data === 1 )