Why does jQuery.ajax() add a parameter to the url?
Cache needs to be true.
Any suggestions as why this will not work? It won;t print the console.logs showing that this wont even allow me in. This call is appending my request URL with &_=1396146406542. How do I get rid of that add-on? So the request URL is
chatlist.php?PHPSESSID=a8f2b228d783642848da94699ce85c03&_=1396146406542
and I just want it to be
chatlist.php?PHPSESSID=a8f2b228d783642848da94699ce85c03&
$.ajax({
url: "chatlist.php?PHPSESSID=a8f2b228d783642848da94699ce85c03",
cache: false,
success: function(data){
window.console && console.log("JSON Received"); //wont print
That is because of cache: false. It adds the timestamp to avoid caching
jquery.ajax doc:
cache (default: true, false for dataType 'script' and 'jsonp')
Type: Boolean
If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.
Set it to:
url: "chatlist.php?PHPSESSID=a8f2b228d783642848da94699ce85c03",
cache: true <--
Related
I'm using codeigniter, I am curious about the get query for long polling I made
function check_new_notif(){
$.ajax({
type:"GET",
url:"/MAIN/AJAX/new_unotif",
async:true,
cache:false,
datatype: "text",
timeout:20000,
success: function(dat){
show_new_notif(dat);
fetch_new_notif();
setTimeout(
check_new_notif,10000
);
},
error: function(XMLHttpRequest,textstatus,errorThrown){
show_new_notif("error");
setTimeout(
check_new_notif,10000
);
}
});
what is the number used for?
when my long polling request on server the links in firebug was like this
GET /MAIN/Ajax/notification?_=1466062273034
and the next call its /MAIN/Ajax/notification?_=1466062273035 ,incremented by one
Anyone know what this ?_=1466062273035 query means?
Thankies
When you set cache: false, it will append timestamp to your URL
Doc: http://api.jquery.com/jquery.ajax/
If set to false, it will force requested pages not to be cached by the
browser. Note: Setting cache to false will only work correctly with
HEAD and GET requests. It works by appending "_={timestamp}" to the
GET parameters. The parameter is not needed for other types of
requests, except in IE8 when a POST is made to a URL that has already
been requested by a GET.
I'm having a bizarre issue where my POST request is being treated as a GET - this only happens on the LIVE environment and works fine locally. I have the correct POST route set up in laravel.
Would there be a case where jQuery would default to GET on a server environment - I'm currently accessing the site via an IP rather than the domain while the DNS resolves, could this perhaps cause an issue?
Route::post('/ajax/sale/filter', 'SalesController#ajaxFilterOptions');
$.ajax({ url: '/ajax/sale/filter/',
data: {filter: options, sale_id: window.saleId, outlet_type: outletType},
type: 'POST',
cache: false,
dataType: 'JSON',
success: _.bind(function (data) {
console.log(data)
}, this)
});
Your code lines up fine, you may want to check your htaccess file to see if a rewrite is happening (or corresponding rewrite with another httpd), or switch to $.post instead of $.ajax to make sure/force it as a POST request.
I know there is an accepted answer here. But whoever comes this page might be interested in knowing that if you remove the leading slash this will work fine. So in the example given, use url "/ajax/sale/filter" instead of "/ajax/sale/filter/"
I have the following ajax request:
var value = $.ajax({
type: "POST",
url: "url.php",
data: { $(someform).serialize(), something: test_number },
cache: false,
async: true
}).success(function(data){
alert("success!");
}).error(function() {
console.log("FAILED");
});
But it is logging FAILED although the url is right. What happens is that the page refreshes the page and the php query isn't done. I guess there are no errors within the url... any idea on why this happens?
You are kind of mixing methods to send your POST data. You can't serialize a query strong and then also append additional data to it using javascript object construct. You will likely need to manually append the last data element to the query string like this:
data: $(someform).serialize() + '&something=' + encodeURIComponent(test_number),
Of course there could still be a problem on the server-side script which is causing a non-200 HTTP response code (and triggering error handler). You just need to fix this first, and if you still have a problem, debug the server-side issue.
I wrote basicly ajax function in my project and then i opened firebug and i saw this number
I want to know what is number is sending or what is meaning of number. Thanks you very much.
Example Code :
$(".index_page").click(function(){
$.ajax({
url: "ajax/index.php",
cache: false
}).done(function( raw_html ){
$("#content").empty();
$("#content").append(raw_html);
});
});
From the Jquery documnetaiton
cache (default: true, false for dataType 'script' and 'jsonp')
Type: Boolean
If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.
That is because of cache: false - This will add a unique parameter (such as current time) into your request url just to make sure browser do not serve the request from cache
This number is there to provide actual results, not cached. It appears when you set cache: false in you ajax request.
My ajax function has stopped working all of a sudden.
function get_file_info()
{
$.ajax({
type: "GET",
url: "http://localhost/includes/get_file_info.php",
dataType: "json",
jsonp: false,
jsonpCallback: "callbackName",
success: function(data) {
return data;
}
});
}
I did some debugging and found that the ajax request is going to
http://localhost/includes/get_file_info.php?_=1297356964250
I am just would like to know what it is and can be used for, also how to remove so the ajax request is like below so it works again.
http://localhost/includes/get_file_info.php
Many Thanks
If you add:
cache: true,
to your call it will remove the timestamp which is there to always call a different URL's so the browser doesn't cache the result. This is standard for calls except for datatypes script and jsonp.
As others have stated though, it would be good to change the server side to stop turning away anything with GET's, maybe check if the get is a _ and only numeric, if not then turn it away...
The "_=1297356964250" query string is jQuery's method of preventing the URL being cached and returning an old result. Adding this ensures that you're retrieving a new response every time.
This is not the cause of your request breaking down. It must be from another issue. Have you tried logging your response and seeing what it returns?
success: function(data) {
console.log(data);
}