繁体   English   中英

通过AJAX从PHP返回值到JS

[英]Return value from PHP to JS via AJAX

我究竟做错了什么? AJAX成功似乎根本没有收到任何提示,因为三个警报均未显示。 该过程有效,但我没有收到任何回复

jQuery.ajax({
  type: 'POST',
  url: 'https://xxxxxxxxxxx.com/charge.php',
  data: {
    tokenid: token.id,
    email: customer_email,
    amount: amount,
    description: customer_first_name + ' ' + customer_surname + ' | ' + reference
  },
  dataType: 'json',
  success: function(response) {
    alert(response);

    if (response == "OK") {
      alert('Payment successfully made! ');
    } else {
      alert('Payment could not be processed. Please try again.');
      location.reload();
    }
  }
});
<?php
  require_once('./stripe/config.php');

  $token  = $_POST['tokenid'];
  $email = $_POST['email'];
  $amount = $_POST['amount'] ;
  $description = $_POST['description'] ;

  $err = 'OK' ;

  $customer = \Stripe\Customer::create(array(
      'email' => $email,
      'source'  => $token
  ));

  try { 
    $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'GBP',
      'description' => $description
    ));
  } catch(\Stripe\Error\Card $e) {
    $err = "Declined - $e";
  }

  function response() {
    global $err;
    print $err ;
    return $err;
  }
  exit response() ;
?>

请帮助,因为这使我发疯。

删除响应功能,打印错误

 <?php
  require_once('./stripe/config.php');

  $token  = $_POST['tokenid'];
  $email = $_POST['email'];
  $amount = $_POST['amount'] ;
  $description = $_POST['description'] ;

  $err = 'OK' ;

  $customer = \Stripe\Customer::create(array(
      'email' => $email,
      'source'  => $token
  ));

 try { $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'GBP',
      'description' => $description
  ));
    } catch(\Stripe\Error\Card $e) {
    $err = "Declined - $e";
    }
echo $err;
?>

将dataType设置为text dataType: 'text',

您的jQuery AJAX请求已设置为接收JSON(通过dataType属性),但您正在返回一个字符串。 该字符串也会重复多次,并且response()函数非常多余。

要解决此问题,请修改您的PHP代码以实际返回JSON,并修改您的jQuery代码以正确读取该JSON。 尝试这个:

$success = true;
$err = '';

$customer = \Stripe\Customer::create(array(
  'email' => $email,
  'source'  => $token
));

try {
  $charge = \Stripe\Charge::create(array(
    'customer' => $customer->id,
    'amount'   => $amount,
    'currency' => 'GBP',
    'description' => $description
  ));
} catch(\Stripe\Error\Card $e) {
  $success = false;
  $err = "Declined - $e";
}

echo json_encode(array('success' => $success, 'err' => $err));
jQuery.ajax({
  type: 'POST',
  url: 'https://xxxxxxxxxxx.com/charge.php',
  data: {
    tokenid: token.id,
    email: customer_email,
    amount: amount,
    description: customer_first_name + ' ' + customer_surname + ' | ' + reference
  },
  dataType: 'json',
  success: function(response) {
    if (response.success) {
      alert('Payment successfully made! ');
    } else {
      console.log(response.err);
      alert('Payment could not be processed. Please try again.');
      location.reload();
    }
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM