Jquery ajax error - Requests adding GET var - php

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

Related

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

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.

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.

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.

Codeigniter echo/var_dump not working

Why doesn't var_dump work here, the ajax call is successful, but there is nothing printed, not even a string literal print from the PHP.
My controller
function check_links() {
$matches = $this->input->get('matchesJSON');
var_dump($matches);
//$this->load->view('publish_links_view');
}
Ajax call
$.ajax({
type: 'GET',
dataType: 'json',
cache: false,
data: 'matchesJSON='+matchesJSON,
url: 'publishlinks/check_links',
success:
function(response) {
}
})
I assume you're expecting it to var_dump to the browser.
Ajax happens "behind the scenes", so it wouldn't output to your browser, you'd have it in your success handler's response argument.
if you want to test it just hit the url directly with your browser.
http://ciroot/index.php/publishlinks/check_links?matchesJSON=test%20text
Also, you can monitor all of your AJAX requests / responses with the Browser extension Firebug, very useful in situations like this.
it seems to be a problem with your ajax call, not with codeigniter
start with removing dataType: 'json' - jQuery will find type of content itself as it's not json
and you need to output response in your function:
function(response) { window.alert(response) }

Jquery ajax request not working in IE?

I'm experiencing a strange problem in IE with an ajax request. It works fine in FF and Chrome, but for some reason no data is added to the resultsPage, slideInResults is called, and the empty page appears. Inspecting it I can see there is nothing there. What can I do to fix this?
$.ajax({
type: "GET",
url: "library/ajax/search.php",
data: dataString,
cache: false,
dataType: "html",
success: function(html){
$('#resultsPage').html(html);
slideInResults();
}
});
Check all tag and case of variables. They creates problem in IE if not properly close or in different case.
Use Fiddler to verify if the request is being sent. If it is, verify that the values sent are correct and finally, verify that the PHP page is not returning a bad response.
Report the response of the PHP page by using an Alert. This will assist with you identifying the source of the issue.

Categories