开发者

How to deal with response data in ajaxComplete event

开发者 https://www.devze.com 2023-01-05 01:28 出处:网络
Say I have this code: function onComplete(event, request, settings) { // How do I get the data? In the correct format?

Say I have this code:

function onComplete(event, request, settings)
{
    // How do I get the data? In the correct format?
}

$('body').ajaxComplete(onComplete);

In regular ajax success handlers, I can just access the data directly since it will be the first parameter to the handler. It will also be in the correct format (as lon开发者_运维问答g as the content-type was set right on the server).

How do I deal with the data on the ajaxComplete event?


You can use it like this, but it's not documented:

function onComplete(event, request, settings) {
  var data = $.httpData(request, settings.dataType, settings);
}

The jQuery.httpData function is what's used internally to get data for the success handler, but you can use it directly. Please be aware that it is undocumented, and therefore subject to change without notice in new releases. For example in jQuery 1.4.3, it will be jQuery.ajax.httpData instead.


According to the the doc:

http://api.jquery.com/ajaxComplete/

I don't think you mean to fiddle with the data, because it doesn't pass any data to the handler. If you want data you better off using the set success property in regular Ajax.


This may not be the right handler to use if you want to get data as this is really intended more as a basic notification callback (for all hooked elements) when any AJAX calls completes.

To get to your data you should you might need to be more targeted in your approach and use the $.ajax() call or one of its variants like $.get() or $.getJSON(). See here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号