Ajax get request with useless parameter

假如想象 提交于 2019-12-11 19:16:36

问题


I'm trying to execute next:

$.ajax({
type: 'GET',
url: 'http://127.0.0.1:6789/dir', 
data: "",   
success: function(data) { /*do something*/ },
dataType: 'html'
});

But when it executes, my server receives something like below:

http://127.0.0.1:6789/dir?_32567871112

I don't want to pass any parameters. What do I wrong?


回答1:


In short, set cache to true in your $.ajax call's options.

jQuery adds that for cache breaking.

There is an option in jQuery to turn that off: (from http://api.jquery.com/jQuery.ajax/)

cache

Default: true, false for dataType 'script' and 'jsonp'

If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.

Example with cache set to true:

$.ajax({
  type: 'GET',
  cache: true,
  url: 'http://127.0.0.1:6789/dir',
  data: "",
  success: function (data) { /*do something*/
  },
  dataType: 'html'
});



回答2:


Check jQuery.ajax documentation

If you look at cache parameter, you can see that it is adding a timestamp at the end of call in that format. If you want to get rid of it try to set cache to true, or type to POST(in case you dont want to allow cashing)




回答3:


I had ajaxSetup attribute in my code:

$.ajaxSetup({ cache: false });

I commented it and it works now! Thanks.



来源:https://stackoverflow.com/questions/10425215/ajax-get-request-with-useless-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!