【问题标题】:Delete return message before php die to ajax在php死到ajax之前删除返回消息
【发布时间】:2021-01-31 04:09:43
【问题描述】:

我用 ajax 调用 php 文件。在这个文件中,我有一个尝试捕获库的可能错误。

当我收到错误消息时,我会执行带有正确消息的 die 函数,但在 ajax 对象的成功方法中,我会收到一些带有错误消息的 html 表。我认为这条消息来自图书馆。

是否可以停止或清除此返回,并发送我自己的消息?

    try {
        $card_id = $stripe->customers->createSource($stripe_account['xxxx'],['source' => $card_token['id']]);
    } catch (Exception $e) {
        echo $e->getMessage();   //show message correctly
        return "ERROR";
    }

阿贾克斯:

$.ajax({
url: '/intranet/pms/includes/pay.php',
type: 'POST',
data: payment,
beforeSend: () => {

},
success: function(data){
    console.log(data)
},
error: function (jqXHR, exception) { 
console.log("ERROR")   
}    
});

在这种情况下,我从来没有得到错误字符串,只有一个字符串中的 html 表:

<br />
<font size='1'><table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught (Status 402) (Request req_SSeWHpDt8WA4GV) The card number is not a valid credit card number.
  thrown in C:\MAMP\htdocs\vendor\stripe\stripe-php\lib\Exception\ApiErrorException.php on line <i>38</i></th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Stripe\Exception\CardException: The card number is not a valid credit card number. in C:\MAMP\htdocs\vendor\stripe\stripe-php\lib\Exception\ApiErrorException.php on line <i>38</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.4018</td><td bgcolor='#eeeeec' align='right'>404976</td><td bgcolor='#eeeeec'>{main}(  )</td>

总是在成功的 ajax 方法中接收此数据,从不出错..

【问题讨论】:

  • 欢迎来到 Snack Overflow,请编辑您的问题并显示您的 AJAX 调用以及正确格式化您的 HTML 块。请删除任何不必要的代码标记,以便我们更容易看到发生了什么。谢谢。
  • 谢谢!在这种情况下,html 块是一个控制台字符串,对于问题来说并不重要。

标签: php ajax stripe-payments


【解决方案1】:

该错误输出来自 xdebug,这是您应该在开发中而不是在生产中安装的东西。话虽如此,异常在 Stripe Api 中。

这里是展示如何捕获它们可以抛出的各种异常的页面:https://stripe.com/docs/api/errors/handling

这实际上是示例代码:

try {
  // Use Stripe's library to make requests...
} catch(\Stripe\Exception\CardException $e) {
  // Since it's a decline, \Stripe\Exception\CardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '\n';
  echo 'Type is:' . $e->getError()->type . '\n';
  echo 'Code is:' . $e->getError()->code . '\n';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '\n';
  echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}

【讨论】:

    【解决方案2】:

    您可能没有捕捉到Stripe\Exception\CardException 异常,请确保在类声明之前有use Exception,或者只是将catch(Exception $ex) 更改为catch(\Exception $ex),希望这可行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2011-05-04
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      相关资源
      最近更新 更多