Ajax Core XMLHttpRequest Summary _ajax related

Source: Internet
Author: User

Ajax: "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML), a comprehensive technique: asynchronous data Exchange using JavaScript object XMLHttpRequest JavaScript operations DOM implements dynamic effects, utilizes XHTML+CSS expression information, and XML and XSLT operation data. This article focuses on asynchronous data exchange with the server side using the XMLHttpRequest object.

How to use
XMLHttpRequest Five-Step use method:

Copy Code code as follows:

1. Create objects;
2. Register callback function;
3. Use the Open method settings and server interaction basic information;
4. Set the data to send, start and server-side interaction;
5. Implement callback function.

Because every time you apply the XMLHttpRequest object, you have to do five steps, so you can encapsulate the use of the object as a JS file, and pass some of the parameters using its method to complete the function, as follows:

Copy Code code as follows:

Use encapsulation method personnel to provide only HTTP requests, URL addresses, data, successes, and failed callbacks
1. Define the construction method of the XMLHttpRequest object
var myxmlhttprequest =function () {
var XMLHttpRequest;
if (window. XMLHttpRequest) {
Ie7,ie8,firefox,mozillar,safari,opera
Alert ("Ie7,ie8,firefox,mozillar,safari,opera");
XMLHttpRequest = new XMLHttpRequest ();
Resolve the problem that the browser may not work on the server side because it does not have a text header
if (Xmlhttprequest.overridemimetype) {
Xmlhttprequest.overridemimetype ("Text/xml");
}
}else if (window. ActiveXObject) {
Ie6,ie5.5,ie5
Alert ("Ie6,ie5.5,ie5");
var activexname =["MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
for (Var n=0;n

Extension issues

1. Browser caching
2. Chinese garbled
3. Cross-domain access

For question 1, question 3 can be resolved by changing the URL address. Issue 1 You can add a timestamp at the end of the URL address, and issue 3 is resolved by proxy. Simply add the appropriate judgment before the third step in Send ():

Copy Code code as follows:

Resolving cached conversions: increasing the timestamp
if (Url.indexof ("?") ) >= 0) {
url = url + "&t=" + (new Date ()). ValueOf ();
} else {
url = url + "? t=" + (new Date ()). ValueOf ();
}
Troubleshoot Cross-domain issues
if (Url.indexof ("http://") >= 0) {
Url.replace ("?", "&");
url = "proxy?url=" + url;
}

Issue 3 corresponds to the Proxy server implementation:

Copy Code code as follows:

/**
* Handles the HTTP get method.
*
* @param request servlet Request
* @param response servlet response
* @throws servletexception If a servlet-specific error occurs
* @throws IOException If an I/O error occurs
*/
@Override
protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Gets the parameter, finally gets the request URL address similar to: url = http://192.168.../AJAX/AJAXServer?aa=11&bb=22&cc=33
StringBuilder url = new StringBuilder ();
Url.append (request.getparameter ("url"));
Gets the Cross-domain address URL of the access = Http://192.168.../AJAX/AJAXServer
Enumeration ENU = Request.getparameternames ();
Boolean flag = false; Defines a flag variable that indicates whether the first argument is a concatenation
while (Enu.hasmoreelements ()) {
String paramname = (string) enu.nextelement ();
if (! Paramname.equals ("url")) {
String paramvalue = Request.getparameter (paramname);
Paramvalue = Urlencoder.encode (paramvalue, "utf-8");
if (! Flag) {
Url.append ("?") )。 Append (paramname). Append ("="). Append (paramvalue);
Flag = true;
} else {
Url.append ("&"). Append (paramname). Append ("="). Append (paramvalue);
}
}
}
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
if (URL!= null && url.length () > 0) {
URL connectionurl = new URL (url.tostring ());
BufferedReader reader = new BufferedReader (New InputStreamReader (Connectionurl.openstream (), "Utf-8"));

The above is the entire content of this article, I hope you can enjoy.

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.