Reputation:
I am trying to learn AJAX from w3schools.com . But i can't understand follwing example:
xhttp.open("GET", "demo_get.asp", true);
xhttp.send();
In the example above, you may get a cached result. To avoid this, add a unique ID to the URL:
xhttp.open("GET", "demo_get.asp?t=" + Math.random(), true);
xhttp.send();
Question1: What is cached result and how it is avoided by adding unique ID to the url?
Question2: When we send request(data) to the specific php file we can see data in specific file only by using $_REQUEST global variable ,why we can't use $_GET global variable (when we are sending data by GET Method)?
thanks in advance.
Upvotes: 0
Views: 871
Reputation: 1
QUESTION 1 By cached results, I think they mean that the response retrieved by your XMLHttpRequest object is temporarily stored/cached by your browser. That means that the next time you try to get a response, you will get the old (cached response) unless you are using an id that is different from that of the cached response. In other words, unique ids ensure you are reaching whatever file you are retrieving data from whenever you ask for a response.
Upvotes: 0
Reputation: 623
Web cache simply means that the file stays in the browser, thus speeding up your browsing since you dont need to get the same files over and over.
1: By adding a random string or a timestamp as a parameter the browser treats it as a new file all the time.
remotefile.js?ts=123 is not the same as remotefile remotefile.js?ts=124
2: In php 5.3+ you get the data by looking at the globals for the specific request type.
$_GET holds all get parameters (ie the part of the URL after ?)
$_POST holds an array of all POST.parameters
file_get_contents('php://input') holds the json in a json-request
The reason you can't see attadhed GET-data using $_GET is because a get reuest only handles URL-parameters. If you want to sent data, look into POST.
http://www.w3schools.com/jquery/ajax_post.asp
Upvotes: 1
Reputation: 93163
Cached Result
:
request
(URL) , browser caches response
(result)"demo_get.asp?t=" + Math.random()
:
This will change the URL for each call , so, for example , if demo_get.asp?t=32332
is cached , it will not influence demo_get.asp?t=43948348
. Thanks to Math.random()
According to Method used in client side , you will be able to handle request in th server-side.
GET method :
//client side
xhttp.open("GET", "demo_get.asp?t=" + Math.random(), true);
//server side
$_GET
POST method :
//client side
xhttp.open("POST", "demo_get.asp?t=" + Math.random(), true);
//server side
$_POST
Upvotes: 1