duanmeng1858 2014-03-19 16:55
浏览 46
已采纳

if,else在jquery中用php数据声明

So I have a javascript ajax call like so:

      $.ajax({
        type: "POST",
        url: "/checkout/doCharge",
        data: dataString,
        success: function(data) {
          if(data == '1'){
            $("#CheckoutSubmit").prop('disabled', false);
          }
          else{
            $.growl.error({ message: "Your card was declined! Please try again." });
          }
        }
      });
      return false;

And the php call is:

$response = $this->stripe->charge_card($amount, $card, $desc);

    if($response['paid'] == 'true'){

        echo '1';
    }
    else{
        echo '0';
    }

Right now, the if(data == '1') is not working. it doesn't throw any error either. I am trying to get data as 1 if the paid response is true and 0 if the response is false.

  • 写回答

1条回答 默认 最新

  • doucong7963 2014-03-19 17:07
    关注

    Use $.trim to remove extraneous whitespace around the response.

        success: function(data) {
          data = $.trim(data);
          if(data == '1'){
            $("#CheckoutSubmit").prop('disabled', false);
          }
          else{
            $.growl.error({ message: "Your card was declined! Please try again." });
          }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?