简体   繁体   中英

returning json message on ajax success

I have a jquery function which performs an update via ajax

the update works successfully, (i can check this in firebug network response), but i cant get a response message, (by alert, or by console)

here is my code snippet...

$('#mcbatchsubscribe').on('click', 'a.ajaxresub', function (e) {
    e.preventDefault();
    var email = $(this).attr('emailvalue');
    $.ajax ({
            url: '/admin/ajax/ajax_resubscribe.php',
            data: {email: email},
            dataType: 'json',
            success: function (json) {
                console.log("success", json);
                //alert(json)
            }
            } );

    } );

If i open up firebug, the console returns nothing, - the network header shows the correct ajax path and parameter, the network response shows the ajax success message, 'Resubscribed Success' -what the ajax page does is to run a list subscribe on the email parameter on mailchimp, and this definitely works. here is the ajax snippet...

...if ($rsemail->TotalRows > 0) { // Trigger if there is a matching row

$merge_vars = array(
                'FNAME'=>    $rsemail->getColumnVal("MemberFirstName"),
                'LNAME'=>    $rsemail->getColumnVal("MemberLastName")      
               );   

$MailChimp = new \Drewm\MailChimp($api_key);

$subscriberesult = $MailChimp->call('lists/subscribe', 
            array('id'                  => $list_id,
                  'email'               => array('email'=>$rsemail->getColumnVal("MemberEmail")),
                  'merge_vars'          => $merge_vars,
                  'double_optin'        => true,
                  'update_existing'     => true,
                  'replace_interests'   => false,
                  'send_welcome'        => false
            ));


if ($subscriberesult ['email'] != $rsemail->getColumnVal("MemberEmail")) {
            echo nl2br(($subscriberesult ['code'])."\r\n");
            echo nl2br(($subscriberesult ['name'])."\r\n");
            echo nl2br(($subscriberesult ['error'])."\r\n");
        } else {
            echo 'Resubscribed success';
}

} else {
  echo 'Resubscribe failure - Email not found';

}

I think I am making progress

I changed success: function (json) to complete: function (json)

i now get

Object {readyState: 4, responseText: "Resubscribed success", status: 200, statusText: "OK"}

The problem

Your AJAX request contains the following setting:

dataType: "json"

The response text is not a valid json text. When jQuery tries to parse the response as json -> it fails and skips the success callback...

More about that in jQuery AJAX documentation

Solution (1)

Drop the dataType:'json' from your ajax parameters- it will prevent jQuery from trying to parse your response string to a json object (and fail doing so...)

OR

Solution 2

Change the responseText in your server code to something like:

"{result: 'Resubscribed success'}"

This will pass jQuery's json validation, and your success handler will be invoked.

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