Javascript 我可以在不添加';的情况下发出jQuery JSONP请求吗;?回调=';URL中的参数?

Javascript 我可以在不添加';的情况下发出jQuery JSONP请求吗;?回调=';URL中的参数?,javascript,jquery,ajax,cross-domain,jsonp,Javascript,Jquery,Ajax,Cross Domain,Jsonp,服务器不会接受请求URL中的任何参数,因此我需要删除URL中的所有额外参数,当然我无法控制服务器 jQuery: $.ajax({ type: 'GET', url: 'http://cross-domain.com/the_jsonp_file, jsonpCallback: 'jsonCallback', contentType: 'application/json', cache: 'true', dataType: 'jsonp',

服务器不会接受请求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
jsonCallback_27b2afa5c77c2510({"test": "hello"});
但是我需要这个(没有参数):

编辑:

以下是我的全部情况:

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);
        };
    };
};

奇怪的是,很少有请求成功地调用了jsonCallback。

检查jQuery文档-他们说在ajax参数中使用jsonp:false和jsonpcCallback:“callbackFunction”……比如:

$.ajax({
    url: 'http://cross-domain.com/the_jsonp_file',
    jsonp : false,
    jsonpCallback: 'jsonCallback',
    // contentType: 'application/json', -- you can't set content type for a <script> tag, this option does nothing for jsonp | KevinB
    cache: 'true',
    dataType : 'jsonp'
});
$.ajax({
网址:'http://cross-domain.com/the_jsonp_file',
jsonp:false,
JSONPCCallback:'jsonCallback',
//contentType:'application/json',--您不能为标记设置内容类型,此选项对jsonp | KevinB没有任何作用
缓存:“true”,
数据类型:“jsonp”
});

JSONP需要回调作为URL的一部分。根据描述,我猜您正在访问的服务器不支持JSONP

jQuery向您的文档添加了一个脚本标记,当添加该标记时,将运行API请求,响应将调用代码中的回调函数(这将简化它)

如果你想要更准确的描述,你可以看看维基百科。

每个请求都调用相同的回调
jsonCallback
,所以我认为这就是问题所在

首先,文档中的Javascript:

<script type="text/javascript">
    new Gallery([
        ['http://cross-domain.url/whatever', '27b2afa5c77c2510'],
        ['http://cross-domain.url/whatever', '13df51b2f2801bc1'],
        ['http://cross-domain.url/whatever', '4de326fc9a2c5a24'],
        ['http://cross-domain.url/whatever', '60c266a73ba699bc'],
        ['http://cross-domain.url/whatever', '3db01e95aaf2b9f2'],
        ['http://cross-domain.url/whatever', '94eb17f9b0e1be9c'],
        ['http://cross-domain.url/whatever', 'ca8c5c3c0b8cd5ae'],
        ['http://cross-domain.url/whatever', '6b0f5c5737ee88fd'],
        ['http://cross-domain.url/whatever', '318d8ebb51a97a15'],
        ['http://cross-domain.url/whatever', 'f5028c8b62e81a8b'],
    ]);
</script>
jsonCallback\uquot>之后添加了随机十六进制字符串,以像jQuery的默认回调一样分隔每个请求。

从输入读取随机十六进制字符串并设置为
jsonpCallback

function Gallery(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][0],
                    jsonp : false,
                    jsonpCallback: 'jsonCallback_' + _this.imgs[j][1],
                    cache: 'true',
                    dataType:'jsonp',
                    success: function(json) {
                      // Process
                      console.log(json.test);
                    },
                });
            })(jQuery, i);
        };
    };
};
功能库(imgs){
//imgs是URL的数组
this.imgs=imgs;
this.submit=函数(){
//按钮单击事件触发此方法
这个;
};
这个。_show=function(){
var_this=这个;
对于(变量i=0;i<\u this.imgs.length;i++){
(函数($,j){
$.ajax({
键入:“GET”,
网址:_this.imgs[j][0],,
jsonp:false,
jsonpCallback:'jsonCallback_'+this.imgs[j][1],
缓存:“true”,
数据类型:'jsonp',
成功:函数(json){
//过程
log(json.test);
},
});
})(jQuery,i);
};
};
};
谢谢你@Adam@Kevin B@Dcullen和所有人!:D


p、 s:我只键入了上面的每个源代码,例如,它可能不正确。

服务器完全拒绝带有查询字符串的请求?您最好自己编写JSONP代码,我没有使用jQuery…而是在ajax设置中尝试了
dataType:'script'
,它使URL没有任何参数,但我不知道如何使回调函数工作。我不明白如果不指定回调函数,您希望如何执行JSONP。这是一个脚本标记,传递回调名称的唯一方法是通过URL。我错过了什么?也许它会将其包装在默认回调中?网址是什么?谢谢。它似乎在工作,但我收到了以下错误消息:
uncaughttypeerror:object[object Window]的属性“jsonCallback”不是函数
。成功调用回调函数的元素很少。@Jonypoins您是否使用jQuery 1.5+?这是因为您尚未定义名为jsonCallback的函数。jsonpCallback属性的值需要是一个全局可用的函数,以便在返回响应时执行。是的,服务器不支持JSONP,但我希望使用JSONP使用服务器上的资源。不幸的是,服务器必须支持JSONP才能工作。否则会出现上面提到的脚本错误。你还有两个选择:1。设置代理。2.CORS(这还需要服务器配置)“每个请求都调用相同的回调jsonCallback,所以我认为这就是问题所在。”-这是我的问题。谢谢+1.
jsonCallback_27b2afa5c77c2510({"test": "hello"});
function Gallery(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][0],
                    jsonp : false,
                    jsonpCallback: 'jsonCallback_' + _this.imgs[j][1],
                    cache: 'true',
                    dataType:'jsonp',
                    success: function(json) {
                      // Process
                      console.log(json.test);
                    },
                });
            })(jQuery, i);
        };
    };
};