numbers behind the ajax long polling url /controller/method?_xxxx - php

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.

Related

PHP script not echoing data when called via AJAX

I have been staring at this problem for the past 2 hours and can't seem to fathom it, even after validating that everything loads correctly when scouring the console.
I basically have two sliders on my page which will eventually populate results in a table, every time I change my slider I send an array of two values to my AJAX script:
function update_results(values)
{
$.ajax({
type: "GET",
url: "./app/core/commands/update_results.php",
data: { query : values },
cache: false,
success: function(data) {
// eventually some success callback
}
});
}
The browser successfully finds update_results.php but it does not perform the logic on the page ( I assume it has found the page as the 404 error does not appear in my console. )
At this point in time the script is extremely bare-bones as I'm obviously trying to establish communication between both files:
<?php
$vals = $_GET['values'];
echo $vals;
In this case $vals is never echoed to the page, am I missing something in my AJAX? I know the values enter the function as alerted them out before attaching the PHP script.
Ajax Calls are suffering from Browser Cache. If your browser thinks, that he already knows the content of update.php, he will return the cached content, and not trigger the script. Therefore your
modified code might simply not get executed. (Therefore your insert query wasn't executed)
To ensure this is not happening in your case, it is always a good idea to pass a parameter (timestamp) to the script, so your browser thinks it's another outcome:
function update_results(values)
{
$.ajax({
type: "GET",
url: "./app/core/commands/update_results.php?random_parameter=" + (new Date().getTime());
data: { query : values },
cache: false,
success: function(data) {
// eventually some success callback
}
});
}
This will ensure that - at least - the browser cache is refreshed once per second for update_results.php, no matter what browser cache-settings or server-side cache advices are telling.
when Ajax is done, the success callback is triggered and the output of you php script is saved in data.
you can handle the data like this:
$.ajax({
type: "GET",
url: "./app/core/commands/update_results.php",
data: { query : values },
cache: false,
dataType: "text",
success: function(data) {
document.write( data )
}
});
PHP, running at server, is unaware of what happening at the front-end browser and it simply respond to ajax request as any other normal http request. So the failure of SQL query has nothing to do with javascript, which only responsible for sending ajax request and receiving and handling the response. I guess there's some errors in your php script.

DUPLICATE: $.ajax appending request url

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 <--

Sending multiple variables in ajax

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.

What is meaning of numbers to Sending in ajax function?

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.

Jquery ajax error - Requests adding GET var

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

Categories