$的工作.ajax json回调函数
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
我想你可以创建一个名为myapfilms的本地函数来处理响应。
就像这样,它应该被自动调用。
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怎么说,这是一个bug。在任何情况下,你的代码的URL是错误的,你有inTheatres和inTheaters("re"answers"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 + ", ");
});
}
});
}
注::我是网站的创建者,我建议你迁移到版本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"变量中包含所有信息
- JavaScript回调函数
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何正确地将参数传递给RequireJS回调函数
- 回调函数在python代码中离线
- 从AJAX回调函数中分离数据
- 赋值后的回调函数
- 如何在javascript回调函数中返回多个变量
- 函数表达式,返回回调函数
- 等待回调函数执行
- JavaScript回调函数和Google Feed API
- 如何从ajax成功回调函数中读取javascript变量
- 正在保存JavaScript内部回调函数中的值
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- 如何在javascript中添加带有回调函数的按钮点击事件作为window.conf
- 在Golang回调函数中启用CORS
- 在初始函数完成之前调用回调函数
- jQuery Mobile:如何在$.Mobile.changePage之前运行回调函数
- 即使使用回调函数也无法返回值
- 为什么prototypjs观察到回调函数有绑定