简体   繁体   中英

Delete return message before php die to ajax

I call to php file with ajax. In this file I have a try catch for get posible errors with a library.

When I receive an error, I execute die function with the correct message, but in success method of ajax object, I receive some html table with the error message. I thing that this message it's from library.

It's posible stop or clear this return, and send my own message?

    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:

$.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")   
}    
});

In this case, I never get the ERROR string, only a html table in string:

<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>

Always receive this data in success ajax method, never on error..

That error output is coming from xdebug, which is something you should have installed in development but not in production. With that said, the Exception is in the Stripe Api.

Here is the page showing how to catch the various exceptions they can throw: https://stripe.com/docs/api/errors/handling

This is literally the example code:

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) ,希望这有效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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