jQuery 'If' statement string comparison not working

前端 未结 3 548
眼角桃花
眼角桃花 2021-01-04 09:35

I\'m having a very hard time trying to do something very simple. Here\'s the code:

        if(data == \'success\') {

            alert(\'foo\');

        }          


        
3条回答
  •  旧时难觅i
    2021-01-04 10:01

    You can fix it on the client side using $.trim() like this:

    if($.trim(data) == 'success') {
    

    Or, a better approach would be removing the new-line coming from the server-side, probably an erroneous new-line in your PHP somewhere, check before or after the block, this is most often where they crop up.

    Or, just exit after outputting your content, like this:

    if($update) {
      echo 'success';
      exit();
    }
    

提交回复
热议问题