jQuery的dataFilter响应未定义 [英] jQuery dataFilter response undefined
问题描述
我从一个外部API通过JSONP与jQuery.ajax()获取数据。
I am fetching data from an external api via jsonp with jQuery.ajax().
这是我的ajax设置:
this is my ajax setup:
var ajax_options = {
dataType: 'jsonp',
jsonp: 'callback',
url: url,
data: parameters,
success: function (response) {
console.log(response); // works, prints the correct data
},
dataFilter: function (response, type) {
console.log(response); // prints undefined
console.log(type); //prints "jsonp"
}
};
$.ajax(ajax_options);
我想使用dataFilter函数preprocess我的反应进行错误处理。但是,这个功能的响应参数始终是不确定的。在成功的函数关系。然而,我得到正确的响应数据。
I want to use the dataFilter function to preprocess my response for error handling. But the response argument of this function is always undefined. In the success function however, I get the correct response data.
你知道可能是什么问题?
Do you know what might be the problem?
推荐答案
我可以确认,这个问题是一个痛苦的屁股。有什么需要做的就是用$ .ajaxSetup({});连同转换器:
I can verify that this issue is a pain in the arse. What needs to be done is to use $.ajaxSetup({}); along with the converters:
$。ajaxSetup
http://api.jquery.com/jQuery.ajaxSetup/
然后,你需要使用转换器来处理你的数据类型dataFilters,回调和解析。
Then, you'll need to use Converters to handle your dataType, dataFilters, callbacks, and parsing.
jQuery.Extensions - 转换器
http://api.jquery.com/extending-ajax/#Converters
虽然你不这样做的有无的通过$ .ajaxSetup使用转换器 -
While you don't have to use Converters via $.ajaxSetup -
$.ajax({ converters: 'params go here' });
情景,因为你面对,那就是通过为您的JSONP需要的所有数据已经该转换器适用于它。这应该为您提供基于你期望的结果类型和需要一个更现实的控制。
Because of the scenario you're facing, all data that is passed as your jsonp needs to have this converter applied to it. This should provide you with a more realistic control based on your expected result type and needs.
这篇关于jQuery的dataFilter响应未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!