最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

AJAX error handling for submit function in functions file

programmeradmin0浏览0评论

I have created a custom function in my functions.php to handle form submissions for a Mailchimp subscriber list. I am making an AJAX call in my JavaScript to process the PHP. The problem I am facing is that my AJAX call always returns "success" even when the Mailchimp function is not successful. When the Mailchimp subscribe function is successful it will return code 200, when its unsuccessful it will return code 400. How can I reconfigure my JS so that code 400 will fire the error function in the AJAX call?

Here's part of my PHP function:

curl_setopt($mchAPI, CURLOPT_URL, 'https://' . substr($apiKey, strpos($apiKey, '-') + 1) . '.api.mailchimp/3.0/lists/' . $listID . '/members/' . md5(strtolower($email)));
curl_setopt($mchAPI, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic ' . base64_encode('user:' . $apiKey)));
curl_setopt($mchAPI, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($mchAPI, CURLOPT_RETURNTRANSFER, true);
curl_setopt($mchAPI, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($mchAPI, CURLOPT_TIMEOUT, 10);
curl_setopt($mchAPI, CURLOPT_POST, true);
curl_setopt($mchAPI, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($mchAPI, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($mchAPI);
$responseCode = curl_getinfo($mchAPI, CURLINFO_HTTP_CODE); // This will return '200' if successful or '400' if unsuccessful

Here's my AJAX call:

$.ajax({
    url: postURL, 
    method: 'POST',
    data: serialized,
    success: function(response) {
        // Always ends here even if subscribe is unsuccesful
        alert('SUCCESS!');
    },
    error: function(xhr, ajaxOptions, thrownerror) {
        alert('FAILED ' + thrownerror);
    }
});

I have created a custom function in my functions.php to handle form submissions for a Mailchimp subscriber list. I am making an AJAX call in my JavaScript to process the PHP. The problem I am facing is that my AJAX call always returns "success" even when the Mailchimp function is not successful. When the Mailchimp subscribe function is successful it will return code 200, when its unsuccessful it will return code 400. How can I reconfigure my JS so that code 400 will fire the error function in the AJAX call?

Here's part of my PHP function:

curl_setopt($mchAPI, CURLOPT_URL, 'https://' . substr($apiKey, strpos($apiKey, '-') + 1) . '.api.mailchimp/3.0/lists/' . $listID . '/members/' . md5(strtolower($email)));
curl_setopt($mchAPI, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic ' . base64_encode('user:' . $apiKey)));
curl_setopt($mchAPI, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($mchAPI, CURLOPT_RETURNTRANSFER, true);
curl_setopt($mchAPI, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($mchAPI, CURLOPT_TIMEOUT, 10);
curl_setopt($mchAPI, CURLOPT_POST, true);
curl_setopt($mchAPI, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($mchAPI, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($mchAPI);
$responseCode = curl_getinfo($mchAPI, CURLINFO_HTTP_CODE); // This will return '200' if successful or '400' if unsuccessful

Here's my AJAX call:

$.ajax({
    url: postURL, 
    method: 'POST',
    data: serialized,
    success: function(response) {
        // Always ends here even if subscribe is unsuccesful
        alert('SUCCESS!');
    },
    error: function(xhr, ajaxOptions, thrownerror) {
        alert('FAILED ' + thrownerror);
    }
});
Share Improve this question asked May 15, 2019 at 18:15 user13286user13286 2272 gold badges12 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

How can I reconfigure my JS so that code 400 will fire the error function in the AJAX call?

The error callback in your $.ajax() setup will be fired if the server responded with a HTTP status header other than "OK" (code: 200), so via the PHP function which handles the AJAX request, just send a HTTP status header with the code 400 ("Bad Request") when the $responseCode is 400.

And you can send it using wp_die():

add_action( 'wp_ajax_your-action', function(){
    ...
    $responseCode = curl_getinfo($mchAPI, CURLINFO_HTTP_CODE);

    // Send a response and the HTTP status header.
    if ( '200' == $responseCode ) {
        echo 'Subscribed'; // success message/data

        wp_die(); // here the code defaults to 200
    } else {
        echo 'Not subscribed'; // error message/data

        wp_die( '', 400 );
    }
} );

Or use status_header() to send custom HTTP error message; i.e. other than the default Bad Request when the code is 400:

add_action( 'wp_ajax_your-action', function(){
    ...
    $responseCode = curl_getinfo($mchAPI, CURLINFO_HTTP_CODE);

    // Send a response and the HTTP status header.
    if ( '200' == $responseCode ) {
        echo 'Subscribed'; // success message/data

        status_header( 200, 'OK' );
    } else {
        echo 'Not subscribed'; // error message/data

        status_header( 400, 'Custom HTTP Error' );
    }

    die(); // not wp_die()
} );
发布评论

评论列表(0)

  1. 暂无评论