[英]The working of $.ajax jsonp Callback function
我正在使用http://www.myapifilms.com/imdb/inTheaters来获取数据。 这是从下面的代码生成的查询 。
function sendRequest() {
var parms = "format=JSONP";
// Other parameters
//parms += "&lang=en-us&actors=S";
$("#countries").text("");
$("#actors").text("");
$.ajax({
data: parms,
url: 'http://www.myapifilms.com/imdb/inTheatres',
type: 'get',
dataType: 'jsonp',
beforeSend: function () {alert(this.url);},
success: function (response, textStatus, jqXHR) {
$.each(response, function(index, element){
if (element.directors != undefined) {
$.each(element.directors, function(index, director){
$("#directors").append(director.name + ", ");
});
}
if (element.title != undefined) {
$.each(element.title, function(index, title){
$("#movies").append(title + ", ");
});
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
$("#error").text(textStatus + "; " + errorThrown);
}
});
}
这里的问题是我收到“ parsererror;错误:未调用jQuery1113009284638670545353_1442120413250 ”。
我只是在传递参数中的值,并且可以看到这很好用http://www.myapifilms.com/imdb?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix 。 但是,此http://www.myapifilms.com/imdb/inTheaters?callback=jQuery21308063971860152206_1442321554390&format=JSONP&_=1442321554392&title=matrix没有 。
从后面的查询中可以看到,该应用程序似乎正在返回默认回调“ myapifilms”。 那么,如何在ajax请求中使用默认回调来访问其拥有的数据? 请帮助我实现同样的目标。 提前致谢。
在这里找到我的工作| http://codepen.io/anon/pen/YywLop
我猜您可以创建一个名为myapifilms的本地函数来处理响应。
这样的事情,它应该会自动被调用。
function myapifilms(response) {
$.each(response, function(index, element){
if (element.directors != undefined) {
$.each(element.directors, function(index, director){
$("#directors").append(director.name + ", ");
});
}
if (element.title != undefined) {
$.each(element.title, function(index, title){
$("#movies").append(title + ", ");
});
}
});
}
怎么说@Jaromanda X,这是一个错误。 无论如何,您的代码的URL是错误的,您拥有inTheat re且inTheat er (“ re”和“ er”之间不匹配)。 我测试此代码并工作:
function submit() {
var url = "http://www.myapifilms.com/imdb/inTheaters";
$.ajax({
data: 'format=JSONP',
url: url,
dataType: 'jsonp',
jsonpCallback: "myapifilms",
success: function (response, textStatus, jqXHR) {
$.each(response, function(index, element){
if (element.directors != undefined) {
$.each(element.directors, function(index, director){
$("#results").append(director.name + ", ");
});
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
$("#error").text(textStatus + "; " + errorThrown);
}
});
}
function myapifilms(response) {
$.each(response, function(index, element){
if (element.directors != undefined) {
$.each(element.directors, function(index, director){
$("#results").append(director.name + ", ");
});
}
});
}
PS:我是网站的创建者,我建议您迁移到版本2。
编辑:
版本2中的通话示例:
function submit() {
var url = "http://www.myapifilms.com/imdb/inTheaters";
var params = {
token: 'YOUR_TOKEN',
format: 'json',
callback: 'myapifilms'
};
$.ajax({
url : url,
data : params,
type : 'get',
dataType : 'jsonp',
jsonpCallback: 'myapifilms'
});
}
function myapifilms(json) {
alert(json.data.inTheaters[0].openingThisWeek);
}
在“ json”变量中,您具有所有信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.