简体   繁体   English

ajax成功返回json消息

[英]returning json message on ajax success

I have a jquery function which performs an update via ajax我有一个 jquery 函数,它通过 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.如果我打开 firebug,控制台什么都不返回, - 网络标头显示正确的 ajax 路径和参数,网络响应显示 ajax 成功消息,“重新订阅成功” - ajax 页面所做的是运行列表订阅mailchimp 上的电子邮件参数,这绝对有效。 here is the ajax snippet...这是ajax片段...

...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)我改成成功:函数(json)来完成:函数(json)

i now get我现在得到

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

The problem问题

Your AJAX request contains the following setting:您的 AJAX 请求包含以下设置:

dataType: "json"

The response text is not a valid json text.响应文本不是有效的json文本。 When jQuery tries to parse the response as json -> it fails and skips the success callback...当 jQuery 尝试将响应解析为 json -> 它失败并跳过success回调...

More about that in jQuery AJAX documentation更多关于jQuery AJAX 文档

Solution (1)解决方案 (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...)从您的 ajax 参数中删除dataType:'json'它会阻止 jQuery 尝试将您的响应字符串解析为json对象(并且这样做失败......)

OR或者

Solution 2解决方案2

Change the responseText in your server code to something like:将服务器代码中的responseText更改为:

"{result: 'Resubscribed success'}"

This will pass jQuery's json validation, and your success handler will be invoked.这将通过 jQuery 的 json 验证,并且您的success处理程序将被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM