[英]Delete return message before php die to ajax
我用ajax調用php文件。 在這個文件中,我有一個 try catch 來獲取庫的可能錯誤。
當我收到錯誤消息時,我使用正確的消息執行 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")
}
});
在這種情況下,我永遠不會得到 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方法接收這些數據,永遠不會出錯..
該錯誤輸出來自 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
}
您可能沒有捕捉到Stripe\\Exception\\CardException
異常,請確保在類聲明之前use Exception
,或者只是將catch(Exception $ex)
更改為catch(\\Exception $ex)
,希望這有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.