How to Give Get in Response Status

Asked

Viewed 108 times

0

I’m implementing a Mailchimp API for a website. I would like to use as a condition the status obtained as response by the according server as follows codes below:

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: inserir a descrição da imagem aqui

Any idea? I’ve tried everything...

2 answers

0

@Everton... You have to explicitly 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'
                )
               }

          }
       })
    });
  • Added return, yet I got 'Undefined' in Resp.status.

  • @André in the snippet: 'echo json_encode($result);'. The $result variable is of what type ? is an array ?

  • Cleo, You hit the solution first, just add the return type (dataType: 'json',) to work...

0

Try the following:

success: function(data, textStatus, request) {
    console.log(request.status);
},
complete: function(request, textStatus) {
    console.log(request.status);
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.