Ajax Learning notes 4_jquery in jquery

Source: Internet
Author: User
Caching problem:
What is a caching problem? That is, when the browser's input is the same, that is, the requested URL is the same, so that the browser will read the cache, two of times, the same content, will not interact with the server side.
Workaround: Add a timestamp to the requested URL.
Through my test, IE, 3,602 times user name input is consistent and will not interact with the server side, Firefox, even if two user name input, will still interact with the server side.
That is: Firefox in the server side of the return of the temp value will be added 1,ie, 360 will not change. So plus timestamp in these three browsers will no longer have caching problems.
Modified code:
Ajaxserver.java
Caching problems
Copy Code code as follows:

Cache problem plus the test code input content unchanged IE, 360 times the number does not increase, 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 the Ajaxserver.java and return the TEMP variable to the client while out.println.
In this way, if the client's temp value is joined, it means that the client and server side are interacting, otherwise there is no.
Verify.js
Copy Code code as follows:

<!--cache problem, use time stamp to solve-->
Add timestamp to URL address, cheat browser, do not read cache
function Converturl (URL) {
Get time stamp
var timestamp= (new Date (). valueof ());
To stitch timestamp information to a 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"). HTML (data);
});
}

In order to illustrate the problem by simply asking for verification and introduction of caching issues, the verification method used is jquery encapsulation Ajax to receive server-side text data.
Also, don't forget to modify the method called in ajax.html, change the name to Verifycache in the script above ()
Chinese questions:
There are two ways to solve this problem:
First: The page end uses a encodeURI, server-side with string Name=new string (old.getbytes ("iso8859-1"), "UTF-8");
Chinese 1
Copy Code code as follows:

function verifychinese1{
var url= "Ajaxserver?name=" +encodeuri ($ ("#username"). Val ());
Url=converturl (URL); Cache
$.get (url,null,function (data) {
$ ("#result"). HTML (data);
});
", adding the above method to the Verify.js, while
String Name=new string (old.getbytes ("iso8859-1"), "UTF-8");

Add to the appropriate location in the Ajaxserver.java class. For example, put it behind the PrintWriter code.
Again, in the first way, don't forget to modify the method called in Ajax.html, the name changed to the verifychinese1 in the script above ()
The second type: page end uses two times encodeURI, server end uses string name= Urldecoder.decode (old, "UTF-8");
Chinese 2
Copy Code code as follows:

function Verifychinese2 () {
var url= "Ajaxserver?name=" +encodeuri (encodeURI ($ ("#username"). Val ());
Url=converturl (URL); Cache
$.get (url,null,function (data) {
$ ("#result"). HTML (data);
});
}
", adding the above method to the Verify.js, while
String name= urldecoder.decode (old, "UTF-8");

Add to the appropriate location in the Ajaxserver.java class. For example, put it behind the PrintWriter code.
Again, in the second way, don't forget to modify the method called in ajax.html, and change the name to Verifychinese2 in the script above ()
Need the source code can leave a message.
Through a few days of learning, Ajax dynamic verification of basic understanding, follow-up will be based on the situation to increase the corresponding content.
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.