Ajax study notes in jquery 4

Source: Internet
Author: User

Cache problems:
What is cache problems? That is, when the input content of the browser is the same, that is, the requested URL is the same, the browser will read the cache, and the content of the two requests will not interact with the server.
Solution: Add a timestamp to the request url.
In my test, IE and 360 do not interact with the server when the user names are the same. firefox still interacts with the server even if the user names are the same.
That is, the temp value returned by the server in firefox will be added with 1 each time, and IE and 360 will not change. Therefore, adding timestamps will not cause any caching problems in these three browsers.
Modified code:
AJAXServer. java
Cache Problems Copy codeThe Code is as follows: // the content of the test code added to the cache does not change. The number of IE and 360 does not increase, and firefox will increase.
Integer inte = (Integer) request. getSession (). getAttribute ("total ");
Int temp = 0;
If (inte = null ){
Temp = 1;
} Else {
Temp = inte. intValue () + 1;
}
Request. getSession (). setAttribute ("total", temp );

Add the above Code to AJAXServer. java, and add the temp variable in out. println to return it to the client.
In this way, if the temp value of the client is added, the client interacts with the server. Otherwise, no.
Verify. jsCopy codeThe Code is as follows: <! -- Cache problem solved with timestamps -->
// Add a timestamp to the url address. The browser is cheated and the cache is not read.
Function convertURL (url ){
// Obtain the timestamp
Var timestamp = (new Date (). valueOf ());
// Splice the timestamp information to the url
// Url = "AJAXServer"
If (url. indexOf ("? ")> = 0 ){
Url = url + "& t =" + timestamp;
} Else {
Url = url + "? T = "+ timestamp;
}
Return url;
}
Function verifyCache (){
Var url = "AJAXServer? Name = "+ $ (" # username "). val ();
Url = convertURL (url); // Cache
$. Get (url, null, function (data ){
$ ("# Result" pai.html (data );
});
}

Because I only asked questions about the verification and introduction of the cache, in order to facilitate the Problem description, I used jquery to encapsulate ajax to receive server-side text data.
In the same example, do not forget to modify the method called in ajax.html. Change the name to verifyCache () in the preceding script ()
Chinese questions:
There are two solutions:
First: the page uses encodeURI once, the server uses String name = new String (old. getBytes ("iso8859-1"), "UTF-8 ");
Chinese 1Copy codeThe Code is as follows: function verifychinese1 {
Var url = "AJAXServer? Name = "+ encodeURI ($ (" # username "). val ());
Url = convertURL (url); // Cache
$. Get (url, null, function (data ){
$ ("# Result" pai.html (data );
});
In verify. js
String name = new String (old. getBytes ("iso8859-1"), "UTF-8 ");

Add the appropriate location to the AJAXServer. java class. For example, it can be placed after the code of PrintWriter.
In the same example, In the first formula, do not forget to modify the method called in ajax.html. Change the name to verifychinese1 () in the preceding script ()
Second: the page uses encodeURI twice, and the server uses String name = URLDecoder. decode (old, "UTF-8 ");
Chinese 2Copy codeThe Code is as follows: function verifychinese2 (){
Var url = "AJAXServer? Name = "+ encodeURI ($ (" # username "). val ()));
Url = convertURL (url); // Cache
$. Get (url, null, function (data ){
$ ("# Result" pai.html (data );
});
}
In verify. js
String name = URLDecoder. decode (old, "UTF-8 ");

Add the appropriate location to the AJAXServer. java class. For example, it can be placed after the code of PrintWriter.
In the same example, when the second formula is used, do not forget to modify the method called in ajax.html. Change the name to verifychinese2 () in the preceding script ()
If you need source code, you can leave a message.
After a few days of study, I have a basic understanding of ajax dynamic verification, and will add relevant content as needed in the future.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.