jQuery AJAX JSONP 调用自动添加回调参数。如何删除它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10955213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:01:25  来源:igfitidea点击:

AJAX JSONP call automatically adding callback parameter. How to remove that?

jqueryajaxjsonp

提问by Avisek Chakraborty

I have few Services- with clean-URLs

我的服务很少 - 带有干净的 URL

and while calling each service, the URL pattern is being checked.

在调用每个服务时,正在检查 URL 模式。

Now am calling those URLs via AJAX from another server using JSONP technique.

现在我使用 JSONP 技术通过 AJAX 从另一台服务器调用这些 URL。

But, while calling, its adding callbackand _(timestamp)parameters with service-URLs, automatically.

但是,同时呼吁其加入callback_(timestamp)与服务的URL参数,自动进行。

The timestamp parameter is removed- by adding cache : true. But cant remove the callback parameter.

时间戳参数被删除 - 通过添加cache : true. 但无法删除回调参数。

here is my AJAX calling code-

这是我的 AJAX 调用代码-

$.ajax({
        type: 'GET',
        url : "http://test.com/test/services/getFollowMeHistory/1/1/50",
        dataType:'jsonp',
        cache : true,
        crossDomain : true,
        //jsonpCallback : false,

        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Error occured while loading Loads."+textStatus);
            }
        });
});

Its calling the URL as- http://test.com/test/services/getFollowMeHistory/1/1/50?callback=falseand am getting 404 from service side.

它调用 URL as-http://test.com/test/services/getFollowMeHistory/1/1/50?callback=false并且从服务端收到 404。

My service is returning data as callbackMethod( {..JSON RESPONSE...} ). So, it will automatically call the function callbackMethod(data)in my script. i dont need that callback parameter in my URL.

我的服务将数据作为callbackMethod( {..JSON RESPONSE...} ) 返回。因此,它会自动调用function callbackMethod(data)我的脚本中的 。我的 URL 中不需要该回调参数。

Just need to remove the ?callback=...part from URL

只需?callback=...要从 URL 中删除部分

Plz help.

请帮忙。

回答by lanzz

You should set jsonp: falseand not jsonpCallback: false. You should also explicitly set the jsonpCallbackoption to the callback name you expect to receive from the service.

你应该设置jsonp: false而不是jsonpCallback: false. 您还应该将该jsonpCallback选项显式设置为您希望从服务接收的回调名称。

Reference: http://api.jquery.com/jQuery.ajax/

参考:http: //api.jquery.com/jQuery.ajax/

回答by Fresheyeball

  1. If you set cacheing to trueie will cache the request response, and all subsequent JSONP calls will not return new data.

  2. Without the callback JSONP is unusable, because there is no way to read the response. The callback is the whole point of JSONP.

  3. Your server must be set up to handle a JSONP request. The url you send will not effect the client side. So your problem must be on the server-side. Which is where you should handle it. Making this not a jQuery problem.
  1. 如果true将缓存设置为ie 将缓存请求响应,并且所有后续 JSONP 调用都不会返回新数据。

  2. 没有回调 JSONP 是不可用的,因为没有办法读取响应。回调是 JSONP 的重点。

  3. 您的服务器必须设置为处理 JSONP 请求。您发送的 url 不会影响客户端。所以你的问题一定是在服务器端。这是你应该处理的地方。使这不是 jQuery 问题。

If you are using a custom callback Try this, but a custom callback is not the same as removing the callback:

如果您使用自定义回调试试这个,但自定义回调与删除回调不同:

 jsonpCallback : "callbackMethod"