【发布时间】:2012-10-03 13:34:57
【问题描述】:
服务器不会接受请求 URL 中的任何参数,所以我需要删除 URL 中所有额外的参数,当然我无法控制服务器。
jQuery:
$.ajax({
type: 'GET',
url: 'http://cross-domain.com/the_jsonp_file,
jsonpCallback: 'jsonCallback',
contentType: 'application/json',
cache: 'true',
dataType: 'jsonp',
success: function(json) {
console.log(json);
},
});
JSONP 文件:
jsonCallback({"test": "hello"});
当我发送 Ajax 请求时,URL 如下所示:
http://cross-domain.com/the_jsonp_file?callback=jsonCallback
但我需要这个(不带参数):
http://cross-domain.com/the_jsonp_file
编辑:
这是我的全部情况:
function MyClass(imgs) {
// imgs is array of URLs
this.imgs = imgs;
this.submit = function() {
// button click event triggers this method
this._show();
};
this._show = function() {
var _this = this;
for (var i = 0; i < _this.imgs.length; i++) {
(function($, j) {
$.ajax({
type: 'GET',
url: _this.imgs[j],
jsonp : false,
jsonpCallback: 'jsonCallback',
cache: 'true',
dataType:'jsonp',
success: function(json) {
console.log(_this.imgs[j]);
},
});
})(jQuery, i);
};
};
};
我收到了这个错误信息:
Uncaught TypeError: Property 'jsonCallback' of object [object Window] is not a function
奇怪的是很少有请求成功调用 jsonCallback。
【问题讨论】:
-
服务器完全拒绝带有查询字符串的请求?
-
您最好自己编写 JSONP 代码,而不是使用 jQuery...
-
我在 ajax 设置中尝试了
dataType: 'script',它正在制作没有任何参数的 URL,但我不知道如何使回调函数工作。 -
我不明白您希望如何在不指定回调的情况下执行 JSONP。它是一个脚本标签,传递回调名称的唯一方法是通过 URL。我错过了什么?也许它将它包装在默认回调中?网址是什么?
标签: javascript jquery ajax cross-domain jsonp