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
AJAX JSONP call automatically adding callback parameter. How to remove that?
提问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 callback
and _(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=false
and 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: false
and not jsonpCallback: false
. You should also explicitly set the jsonpCallback
option to the callback name you expect to receive from the service.
你应该设置jsonp: false
而不是jsonpCallback: false
. 您还应该将该jsonpCallback
选项显式设置为您希望从服务接收的回调名称。
Reference: http://api.jquery.com/jQuery.ajax/
回答by Fresheyeball
If you set cacheing to
true
ie will cache the request response, and all subsequent JSONP calls will not return new data.Without the callback JSONP is unusable, because there is no way to read the response. The callback is the whole point of JSONP.
- 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.
如果
true
将缓存设置为ie 将缓存请求响应,并且所有后续 JSONP 调用都不会返回新数据。没有回调 JSONP 是不可用的,因为没有办法读取响应。回调是 JSONP 的重点。
- 您的服务器必须设置为处理 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"