How to give Get in Response Status

0

I am implementing a MailChimp API for a website. I would like to use as condition the status obtained in response by the server according to the following codes:

JQuery / AJAX:

$('#subscribe').on('submit', function (event) {
    var values = {
        "fname": $('#fname').val(),
        "lname": $('#lname').val(),
        "email": $('#email').val()
    };
    $.ajax({
        type: 'POST',
        url: '../includes/subscribe.php',
        data: values,
        success: function (resp) {
           if (resp == 1) {
                swal("Pronto!", "Sua mensagem foi enviada com sucesso!", "success");
                document.getElementById('subscribe').reset()
            } else if (resp.status == 400){
                swal(
                    'Oops...',
                    'Você já está cadastrado...',
                    'error'
                )
            } else {
                swal(
                    'Oops...',
                    'Algo deu errado. Tente novamente...',
                    'error'
                )
            }

        }
    })
});

PHP:     

// Put your MailChimp API and List ID hehe
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$list_id = 'XXXXXXXXXXXXXXXXXXXX';

// Let's start by including the MailChimp API wrapper
include('../includes/MailChimp.php');
// Then call/use the class
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp($api_key);

// Submit subscriber data to MailChimp
// For parameters doc, refer to: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/
// For wrapper's doc, visit: https://github.com/drewm/mailchimp-api
$result = $MailChimp->post("lists/$list_id/members", [
                        'email_address' => $_POST["email"],
                        'merge_fields'  => ['FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"]],
                        'status'        => 'subscribed',
                    ]);

if ($MailChimp->success()) {
    // Success message
    echo 1;
    echo json_encode($MailChimp->getLastResponse());
} else {
    // Display error
    echo json_encode($result);
}

? >

Response:

Any ideas? already tried everything ...

    
asked by anonymous 14.11.2017 / 16:15

1 answer

0

@Everton ... You have to explicitly state that the return will be a json.

    $.ajax({
    type: 'POST',
    url: '../includes/subscribe.php',
    data: values,
    dataType: 'json',
    success: function (resp) {
       if (resp == 1) {
            swal("Pronto!", "Sua mensagem foi enviada com sucesso!", 
       "success");
            document.getElementById('subscribe').reset()
        } else if (resp.status == 400){
            swal(
                'Oops...',
                'Você já está cadastrado...',
                'error'
            )
        } else {
            swal(
                'Oops...',
                'Algo deu errado. Tente novamente...',
                'error'
                )
               }

          }
       })
    });
    
14.11.2017 / 16:37