使用 success/jsonpCallback 与 ajax 请求
我正在使用 Netflix 的 OData API 开发一个应用程序。我关注了 Stephen Walther 的博客条目 了解如何查询 OData API。在其中,他使用以下代码:
$.ajax({
dataType: "jsonp",
url: query,
jsonpCallback: "callback",
success: callback
});
在我的应用程序中,我需要使用 OData 的分页链接来检索完整的列表。我的代码如下:
// create url and handle ajax call to Netflix
function getTitles() {
query = "http://odata.netflix.com/v2/Catalog" // netflix odata base url
+ "/Genres('Television')" // select Genre
+ "/Titles" // top-level resource
+ "?$select=NetflixApiId,Name,BoxArt,Synopsis,ReleaseYear,AverageRating,Series" // choose fields
+ "&$orderby=Name" // Sort results by name
+ "&$filter=Instant/Available eq true" // filter by instant view
+ " and Type eq 'Season'" // select only seasons
+ "&$expand=Series" // include series data
+ "&$callback=callback" // specify name of callback function
+ "&$format=json"; // json request
$.ajax({
dataType: "jsonp",
url: query,
jsonpCallback: "callback",
success: callback,
error: function(XHR, textStatus, errorThrown){
alert(textStatus + ":" + errorThrown);
}
});
}
// create seasons array and and repeat ajax call until all results are returned
function callback(result) {
seasons = seasons.concat(result["d"]["results"]);
if (typeof result["d"]["__next"] != 'undefined') {
var urlJSONP = result["d"]["__next"] + "&$callback=callback&$format=json";
$.ajax({
dataType: "jsonp",
url: urlJSONP,
jsonpCallback: "callback",
success: callback,
error: function(XHR, textStatus, errorThrown){
alert(textStatus + ":" + errorThrown);
}
});
} else {
processResults();
}
}
但是,当运行时我不断收到 parserError
。看来回调函数被调用了两次。如果我删除 success:callback
行,应用程序就可以正常工作。我的问题是:从 ajax 调用中保留 success
代码行是否有问题?或者为什么需要同时包含 jsonpCallback
和 success
行?我问这个主要是出于好奇,因为应用程序似乎在没有两条回调线的情况下工作正常。
I'm developing an application for Netflix using their OData API. I've followed Stephen Walther's blog entry on how to query the OData API. In it, he uses the following code:
$.ajax({
dataType: "jsonp",
url: query,
jsonpCallback: "callback",
success: callback
});
In my application, I need to use the OData's paging links, to retrieve the full listings. My code is as follows:
// create url and handle ajax call to Netflix
function getTitles() {
query = "http://odata.netflix.com/v2/Catalog" // netflix odata base url
+ "/Genres('Television')" // select Genre
+ "/Titles" // top-level resource
+ "?$select=NetflixApiId,Name,BoxArt,Synopsis,ReleaseYear,AverageRating,Series" // choose fields
+ "&$orderby=Name" // Sort results by name
+ "&$filter=Instant/Available eq true" // filter by instant view
+ " and Type eq 'Season'" // select only seasons
+ "&$expand=Series" // include series data
+ "&$callback=callback" // specify name of callback function
+ "&$format=json"; // json request
$.ajax({
dataType: "jsonp",
url: query,
jsonpCallback: "callback",
success: callback,
error: function(XHR, textStatus, errorThrown){
alert(textStatus + ":" + errorThrown);
}
});
}
// create seasons array and and repeat ajax call until all results are returned
function callback(result) {
seasons = seasons.concat(result["d"]["results"]);
if (typeof result["d"]["__next"] != 'undefined') {
var urlJSONP = result["d"]["__next"] + "&$callback=callback&$format=json";
$.ajax({
dataType: "jsonp",
url: urlJSONP,
jsonpCallback: "callback",
success: callback,
error: function(XHR, textStatus, errorThrown){
alert(textStatus + ":" + errorThrown);
}
});
} else {
processResults();
}
}
However, when this runs I keep getting a parserError
. It appears that the callback function is being called twice. If I remove the success: callback
line, the application works fine. My question is: Is there a problem with leaving the success
line of code from the ajax call? Or why would it be necessary to include both the jsonpCallback
and success
lines? I'm mainly asking this out of curiosity, because the application seems to work fine without both callback lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的代码尝试执行的操作,我不确定为什么您在
$.ajax
中同时指定jsonpCallback
和success
称呼。我建议您只指定success
以便处理您的数据并处理您的分页。让 jQuery 定义 jsonp 回调的名称。本质上,jsonp 回调所做的是从 WCF 数据服务接收有效负载,然后将其传递给成功处理程序。如果您想在
success
处理程序处理数据之前对数据进行一些缓存或其他预处理,您似乎可以使用jsonpCallback
。我不确定为什么在这种情况下您要指定与jsonpCallback
和success
处理程序相同的函数。 (我简要浏览了您链接到的 Stephen 的文章,我不是他这样做的原因。)下面是对 WCF 数据服务的示例 jsonp 调用,我在演示和演讲中使用了该服务(并且已经使用了一段时间) )。我使用
JSONPSupportBehaviorAttribute
来在我的 WCF 数据服务中启用 JSONP(不确定您是否正在使用该属性)。但在我的示例代码中,我没有指定 jsonpCallback 名称;我只是指定
jsonp
查询字符串参数(必须是$callback
而不是默认的callback
),但我让 jQuery 命名 jsonp 回调函数。我的
success
处理程序被调用一次,一切正常。所以我的建议是忘记 jsonpCallback ,保留您的成功处理程序,我认为事情应该开始更好地工作。我希望这有帮助。如果您有后续问题,请告诉我。祝你好运!
Based on what your code is trying to do, I'm not sure why you're specifying both
jsonpCallback
andsuccess
in your$.ajax
call. I would suggest you just specifysuccess
in order to process your data and handle your paging. Let jQuery define the name of your jsonp callback.Essentially what the jsonp callback is doing is receiving the payload from your WCF Data Service and then handing it to your success handler. It looks like you could use
jsonpCallback
if you wanted to do some caching or some other pre-processing of the data before it gets handled by yoursuccess
handler. I'm not sure why you'd specify the same function as both yourjsonpCallback
andsuccess
handlers in this case. (I briefly looked through Stephen's article that you linked to, and I'm not why he does this.)Below is a sample jsonp call to a WCF Data Service that I use in demos and talks (and have been using for a little while). I use the
JSONPSupportBehaviorAttribute
in order to enable JSONP in my WCF Data Service (not sure if that's what you're using or not).But in my sample code, I don't specify a
jsonpCallback
name; I just specify thejsonp
querystring parameter (has to be$callback
instead of the defaultcallback
), but I let jQuery name the jsonp callback function.My
success
handler is called once and everything works fine. So my suggestion is to forget aboutjsonpCallback
, keep yoursuccess
handler in place, and I think things should start to work better.I hope this helps. Let me know if you have follow-on questions. Good luck!
不要定义回调,因为 jQuery 会为您创建该函数。这是一个示例,jsFiddle:
callback=?
基本上要求 jQuery 处理所有 JSONP 返回。Don't define
callback
, because jQuery creates that function for you. Here's an example, jsFiddle:callback=?
basically asks jQuery to handle all the JSONP returns.