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.com/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);
    }
});
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()
} );
  • This worked great, thank you! – user13286 May 15 at 19:52
  • You're welcome. :) – Sally CJ May 16 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.