Introduction to Ajax

Source: Internet
Author: User
Tags html form php script

Tag: It's user input parameter add status HTM question what reference

Ajax byHTML, JavaScript? Technology, DHTML, and DOM make this an excellent way to turn a clumsy Web interface into an interactive Ajax application. The author of this article is an Ajax expert who demonstrates how these technologies work together-from a general overview to a discussion of details-to make efficient WEB development a reality. He also unveiled the mysteries of the core Ajax concept, including XMLHttpRequest objects.

Five years ago, if you did not know XML, you were an ugly duckling that nobody attached to. 18 months ago, Ruby became the center of attention, not knowing Ruby'sProgrammers can only be on the bench. Today, if you want to keep up with the latest technology fashion, your goal is Ajax.

However, Ajax is not just a fashion, it is a buildingA powerful way of web sites, and not as difficult as learning a new language.

But before we go into the details of what Ajax is, let's take a few minutes to understand what Ajax does. Currently, there are two basic options for writing an application:

• Desktop Applications
· WEB Application

The two are similar, and desktop applications are typically CD-based (sometimes downloadable from a Web site) and fully installed on your computer. Desktop applications may use the Internet to download updates, but run these applicationsThe code is on the desktop computer. Web application runs somewhere in the webServer--Not surprisingly, to go through the WebThe browser accesses this application.

However, what's more important than where the running code for these applications is is how the application works and how it interacts with it. Desktop applications are generally very fast (running on your computer without waiting for an internet connection), with a nice user interface (usually related to the operating system) and extraordinary dynamics. You can click, select, enter, open menus and submenus, cruise around, and basically don't have to wait.

Web applications, on the other hand, are the latest trends that provide services that cannot be implemented on the desktop (such as Amazon.com and EBay). However, with the powerful Web there is waiting, waiting for the server to respond, waiting for the screen to refresh, waiting for the request to return and generate a new page.

This is clearly said to be abbreviated, but the basic concept is the same. As you might have guessed, Ajax tries to build a bridge between the functionality and interactivity of desktop applications and the ever-evolving WEB application. You can use dynamic user interfaces and nice controls that are common in desktop applications, but in WEB applications.

What are you waiting for? Let's look at how Ajax transforms a clumsy Web interface into an AJAX application that can respond quickly.

Old technology, New tricks

When it comes to Ajax, there are actually multiple technologies involved, and the flexibility to use it requires a deep understanding of these different technologies (the first few articles in this series will discuss these technologies separately). The good news is that you may already be familiar with most of these techniques, and better yet, these techniques are easy to learn and not as difficult as a complete programming language (such as Java or Ruby).

The following are the basic techniques used by AJAX applications:

· HTML is used to establish WEB forms and to determine the fields used by other parts of the application.
· JavaScript code is the core code that runs Ajax applications, helping to improve communication with server applications.
· DHTML or dynamic HTML, which is used to dynamically update the form. We'll use Div, span, and other dynamic HTML elements to tag the HTML.
• The Document Object Model DOM is used (through JavaScript code) to process the HTML structure and (in some cases) the XML returned by the server.

of AjaxDefined

By the way, Ajax is an abbreviation for asynchronous JavaScript and XML (and DHTML, etc.). This phrase was invented by Jesse James Garrett of Adaptive Path (see Resources), according to Jesse'sExplanation, this is not an acronym.

We are here to further analyze the responsibilities of these technologies. I'll delve into these techniques in a future article, so just be familiar with these components and techniques. The more familiar the code is, the easier it will be to shift from a fragmented understanding of these technologies to truly mastering these technologies (and really opening the door to WEB application development).

XMLHttpRequest Object

One of the objects to understand may be the most unfamiliar to you, namely XMLHttpRequest. This is a JavaScript object and it is simple to create the object, as shown in Listing 1.

Listing 1. Create a new XMLHttpRequest object

<script language= "JavaScript "type=" Text/javascript ">
var xmlHttp = new XMLHttpRequest ();
</script>
This object will be discussed further in the next installment, and now you know that this is the object that handles all server traffic. Before you continue reading, stop and think about it: JavaScript is the XMLHttpRequest object to talk to the server. This is not a generic application flow, which is exactly the source of the powerful features of Ajax.

In a generic WEB application, users fill Out form fields and click the Submit button. The entire form is then sent to the server, which the server forwards to the script that processes the form (usuallyPHP or Java, or it could be a CGI process or something like that), the script executes and then sends it back to the new page. The page might be HTML with a new form populated with some data, a confirmation page, or a page with some options selected based on the data entered in the original form. Of course, the user must wait while the script or program on the server processes and returns a new form. The screen becomes blank until the server returns data and then redraws. This is why interactivity is poor, and users don't get immediate feedback, so they feel different from desktop applications.

Ajax basically puts JavaScript technology and XMLHttpRequest objects between Web forms and servers. When a user fills out a form, the data is sent to some JavaScript code instead of being sent directly to the server. Instead, the JavaScript code captures the form data and sends the request to the server. Also, the form on the user's screen does not blink, disappear, or delay. In other words, the JavaScript code sends the request behind the scenes, and the user doesn't even know that the request was made. Better yet, the request is sent asynchronously, meaning that the JavaScript code (and the user) does not wait for the server to respond. So users can continue to enter data, scroll the screen, and use the application.

The server then returns the data to the JavaScript code (still in the Web form), which determines how the data is processed. It can quickly update form data, making it feel that the application is done immediately, that the form is not committed or refreshed, and that the user gets new data. JavaScript code can even perform some sort of calculation on the received data and send another request without user intervention at all! This is the strength of XMLHttpRequest. It can interact with the server as needed, and users can even be completely unaware of what is going on behind the scenes. The result is a dynamic, responsive, highly interactive experience similar to desktop applications, but with all the power of the internet behind it.

Add some JavaScript

With the XMLHttpRequest handle, the other JavaScript code is very simple. In fact, we will use JavaScript code to accomplish very basic tasks:

• Get form data: JavaScript code can easily extract data from an HTML form and send it to the server.
• Modify the data on the form: It is also easy to update the form, from setting the field value to quickly replacing the image.
• Parse HTML and xml: Manipulate the DOM using JavaScript code (see the next section) to work with the structure of the XML data returned by the HTML form server.

For the first two points, you need to be very familiar with the getElementById () method, as shown in Listing 2.

Listing 2. Capturing and setting field values with JavaScript code


Get the value of the "Phone" field and stuff it in a variable called phone
var phone = document.getElementById ("Phone"). Value;

Set Some values on a form using a array called response
document.getElementById ("Order"). Value = Response[0];
document.getElementById ("Address"). Value = Response[1];
There is no special place to pay attention to, it's great! You should realize that there are no very complex things here. As long as the rest of the Xmlhttprequest,ajax application is mastered, the simple JavaScript code shown in Listing 2 is mixed with a small amount of HTML. Also, use a little DOM, and we'll take a look at it.

End With DOM

Finally, there is the DOM, the Document Object model. Perhaps the DOM is a bit daunting for some readers, and the HTML designer seldom uses it, even if the JavaScript programmer doesn't use it, unless it's done with a high-end programming task. The use of DOM in a large number of complex Java and C + + programs is probably why DOM is considered difficult to learn.

Fortunately, using the DOM in JavaScript technology is easy and intuitive. Now, as usual, you might want to explain how to use the DOM, or at least some sample code, but that might also mislead you. Even if you ignore the DOM, you can still delve into Ajax, which is the approach I'm going to take. Future articles will discuss Dom again, and now just know that you might need the DOM. When it comes to passing XML and changing HTML forms between the JavaScript code and the server, we delve into the DOM. You can do some interesting work without it, so put the DOM aside now.

Get the Request object

With the basics above, let's take a look at some concrete examples. XMLHttpRequest is at the heart of Ajax applications and may be unfamiliar to many readers, so let's start here. As you can see from listing 1, creating and using this object is very simple, isn't it? Wait a minute.

Remember those nasty browser wars years ago? There is no one thing that gets the same result on a different browser. Whether you believe it or not, these wars are still going on, albeit on a smaller scale. But oddly enough, XMLHttpRequest became one of the victims of the war. Therefore, obtaining XMLHttpRequest objects may require different methods. I'll explain it in more detail below.

Using Microsoft Browser

Microsoft Browser Internet Explorer uses the MSXML parser to process XML (you can learn more about MSXML through resources). So if you're writing an AJAX application that's dealing with Internet Explorer, you have to create the object in a special way.

But not so simple. Depending on the version of JavaScript technology installed in Internet Explorer, MSXML actually has two different versions, so you must write the code separately for both cases. See listing 3, where the code creates a xmlhttprequest on a Microsoft browser.

Listing 3. Create a XMLHttpRequest object on a Microsoft browser

var xmlHttp = false;
try {
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
} catch (e) {
try {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (E2) {
XmlHttp = false;
}
}
You may not fully understand the code, but it doesn't matter. At the end of this series of articles, you will have a deeper understanding of JavaScript programming, error handling, conditional compilation, and so on. Now just remember the two lines of code firmly:

XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");

And

XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");.

These two lines of code basically attempt to create an object using one version of MSXML, and if it fails, the object is created with another version. Isn't that good? If none are successful, set the XmlHttp variable to False to tell you that the code is having problems. If this is the case, it may be because a non-Microsoft browser is installed that requires different code.

Working with Mozilla and non-Microsoft browsers

If you choose a browser other than Internet Explorer, or write code for a non-Microsoft browser, you need to use different code. In fact, this is the simple line of code shown in Listing 1:

var xmlHttp = new XMLHttpRequest object;.

This is a much simpler code that creates XMLHttpRequest objects in Mozilla, Firefox, Safari, Opera, and virtually all non-Microsoft browsers that support Ajax in any form or manner.

Combined.

The key is to support all browsers. Who wants to write an application that can be used only for Internet Explorer or non-Microsoft browsers? Or worse, write an application two times? Of course not! Therefore, the code supports both Internet Explorer and non-Microsoft browsers. Listing 4 shows this code.

Listing 4. Create XMLHttpRequest objects in a way that supports multiple browsers


/* Create A new XMLHttpRequest object to talk to the WEB server */
var xmlHttp = false;
/* @cc_on @*/
/* @if (@_jscript_version >= 5)
try {
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
} catch (e) {
try {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (E2) {
XmlHttp = false;
}
}
@end @*/

if (!xmlhttp &amp;& typeof XMLHttpRequest! = ' undefined ') {
XmlHttp = new XMLHttpRequest ();
}
Now, regardless of the strange symbols that are commented out, such as @cc_on, this is a special JavaScript compiler command that will be discussed in detail in the next installment of the XMLHttpRequest article. The core of this code is divided into three steps:

1. Create a variable xmlHttp to reference the XMLHttpRequest object that will be created.
2. Try to create the object in Microsoft browser:
1) Try to create it using the Msxml2.xmlhttp object.
2) If it fails, try microsoft.xmlhttp the object again.
2. If XmlHttp is still not established, the object is created in a non-Microsoft manner.
Finally, XmlHttp should refer to a valid XMLHttpRequest object, no matter what browser it runs.

A little description of security

What about security? Browsers now allow users to increase their level of security, turn off JavaScript technology, and disable any options in the browser. In this case, the code will not work anyway. The problem must be dealt with properly at this point, which needs to be discussed in a separate article, to be put in the future (is this series long enough?) Don't worry, you may have mastered it before you finish reading it. Writing a robust but not perfect code now is good for mastering Ajax. We will discuss more details in the future.

Request/Response in the Ajax world

Now that we have introduced Ajax, we have a basic understanding of XMLHttpRequest objects and how to create them. If you read carefully, you may already know that you are dealing with a WEB application on a server that is JavaScript technology, rather than an HTML form that is submitted directly to that application.

What is missing? How to use XMLHttpRequest. Because this code is very important, every AJAX application you write is going to use it in some form, first look at the basic request/response model of Ajax.

Make a request

You already have a brand new XMLHttpRequest object, now let it do some work. First, you need a JavaScript method that the Web page can invoke (such as when the user enters text or selects an item from the menu). The next step is basically the same process in all Ajax applications:

1. Get the data you need from the Web form.
2. Establish the URL to connect to.
3. Open the connection to the server.
4. Set the function to run when the server is finished.
5, send the request.

The example Ajax method in Listing 5 is organized in this order:

Listing 5. Make an Ajax request


function CallServer () {
Get the city and state from the Web Form
var city = document.getElementById ("City"). Value;
var state = document.getElementById ("state"). Value;
Only go on if there is values for both fields
if (city = null) | | (City = = "")) Return
if (state = = NULL) | | (state = = "")) Return

Build the URL to connect to
var url = "/scripts/getzipcode.php?city=" + Escape (city) + "&state=" + Escape (state);

Open a connection to the server
Xmlhttp.open ("GET", url, True);

Setup a function for the server to run when it's done
Xmlhttp.onreadystatechange = Updatepage;

Send the request
Xmlhttp.send (NULL);
}
Most of the code has a clear meaning. The starting code uses the basic JavaScript code to get the values of several form fields. Then set a PHP script as the target of the link. To notice how the script URL is specified, city and state (from the form) are appended to the URL with a simple GET parameter.

Then open a connection, which is the first time you see Using XMLHttpRequest. It specifies the connection method (GET) and the URL to connect to. The last parameter, if set to true, will request an asynchronous connection (this is the origin of Ajax). If False is used, the code will wait for the response returned by the server after the request is made. If set to True, the user can still use the form (or even call other JavaScript methods) when the server processes the request in the background.

XmlHttp (Remember, this is the XMLHttpRequest object instance) onReadyStateChange property tells the server what to do after the run is complete (it may take five minutes or five hours). Because the code does not wait for the server, you must let the server know what to do so that you can respond. In this example, if the server finishes processing the request, a special method named Updatepage () is triggered.

Finally, use the value NULL to call Send (). Since the data to be sent to the server has been added to the request URL (city and state), no data is sent in the request. This makes a request and the server works according to your requirements.

If you don't find anything new, you should realize how simple and straightforward it is! In addition to remembering the asynchronous nature of Ajax, these things are pretty straightforward. Be thankful that Ajax allows you to concentrate on writing beautiful applications and interfaces without worrying about complex HTTP request/response codes.

The code in Listing 5 illustrates the ease of use of Ajax. The data is simple text and can be used as part of the request URL. Send a request with a GET instead of a more complex POST. There is no data to be sent in the request body without XML and the content header to be added; in other words, this is the utopia of Ajax.

Don't worry, things are going to get more complicated as this series of articles unfolds. You'll see how to send a POST request, how to set the request header and content type, how to encode the XML in the message, how to increase the security of the request, and many more things you can do! For the time being, don't worry about the difficulties, master the basic things on the line, and soon we will build a set of Ajax tool Library.

Handling Responses

Now it's time to face the server's response. Now just know two points:

• Do nothing until the value of the Xmlhttp.readystate property is equal to 4.
• The server populates the response into the Xmlhttp.responsetext attribute.

The 1th, the ready state, will be discussed in more detail in the next article, and you will learn more about the phase of the HTTP request than you might have imagined. Now just check for a specific value (4) (more values are available in the next installment). 2nd, use the Xmlhttp.responsetext property to get the server's response, which is simple. The example method in Listing 6 is available for the server to call based on the data sent in Listing 5.

Listing 6. Handling Server Responses

function Updatepage () {
if (xmlhttp.readystate = = 4) {
var response = Xmlhttp.responsetext;
document.getElementById ("ZipCode"). Value = response;
}
}
The code is neither difficult nor complex. It waits for the server to call and, if it is ready, sets the value of another form field using the value returned by the server (here is the ZIP code of the city and state entered by the user). The ZipCode field that contains the ZIP code suddenly appears, and the user does not press any button! That's what the desktop app feels like before. Fast response, dynamic feel, and so on, all because of a small piece of Ajax code.

The attentive reader may notice that ZipCode is an ordinary text field. Once the server returns a ZIP code, the Updatepage () method sets the value of that field in the city/State zip code, and the user can overwrite the value. There are two reasons to do this: Keep the example simple, stating that sometimes you might want users to be able to modify the data returned by the server. To remember these two points, they are important for good user interface design.

Connect Web Forms

What else is there? Not much, actually. A JavaScript method captures the user's input form information and sends it to the server, another JavaScript method listens and processes the response, and sets the value of the field when the response returns. All of this actually relies on invoking the first JavaScript method, which starts the whole process. The most obvious way is to add a button to the HTML form, but this is the 2001 approach, don't you think? Use JavaScript technology like Listing 7.

Listing 7. Start an Ajax process

<form>
<p>city: <input type= "text" name= "City" id= "City" size= "25"
Onchange= "CallServer ();"/></p>
<p>state: <input type= "text" name= "state" id= "state" size= "25"
Onchange= "CallServer ();"/></p>
<p>zip Code: <input type= "text" name= "ZipCode" id= "City" size= "5"/></p>
</form>
If it feels like a fairly common code, that's right, that's it! When the user enters a new value in the City or state field, the CallServer () method is triggered, and Ajax starts to run. Kinda understand what's going on? All right, that's it!

Conclusion

Now you're probably ready to start writing your first Ajax application, or at least want to read the articles in resources? But you can start with a basic idea of how these applications work, and have a basic understanding of XMLHttpRequest objects. In the next installment, you'll learn how to handle JavaScript and server communication, how to use HTML forms, and how to get DOM handles.

Now take some time to think about how powerful Ajax applications are. Imagine how a Web form responds immediately when you click a button, enter a field, select an option from a combo box, or drag a mouse over the screen. Think about what it means to be asynchronous, think of the JavaScript code running and not wait for the server to respond to its request. What kind of problems will you encounter? What kind of field will it enter? Given this new approach, how do you change the design of a form when programming?

If you spend a bit of time on these issues, you'll get more revenue than simply cutting/pasting some code into an application that you don't understand at all. In the next installment, we'll put these concepts into practice, detailing the code needed to make the application work in this way. So, let's enjoy the possibilities of Ajax now.


Page 2nd using JavaScript and Ajax to make asynchronous requests

Most WEB applications use the request/response model to obtain a full HTML page from the server. It is often a click of a button, wait for the server to respond, then click on another button, and then wait, such a repetitive process. With Ajax and XMLHttpRequest objects, you can use a request/response model that does not require the user to wait for the server to respond. In this article, Brett McLaughlin describes how to create XMLHttpRequest instances that adapt to different browsers, establish and send requests, and respond to servers.

In the previous installment of this series (see Resources for a link), we introduced AJAX applications and examined the basic concepts of driving AJAX applications. The core is a lot of the techniques you might already know: JavaScript, HTML and XHTML, a little Dynamic HTML, and the DOM (Document Object model). This article will zoom in on one point and focus on specific Ajax details.

In this article, you will begin to touch the most basic and fundamental Ajax-related objects and programming methods: XMLHttpRequest objects. The object is actually just a common thread that spans all Ajax applications, and you may have expected that you can fully exploit the potential of programming only if you thoroughly understand the object. In fact, sometimes you will find that to use XMLHttpRequest correctly, you obviously can't use XMLHttpRequest. What the hell is going on here?

Web 2.0 at a glance

Take a look at recent ideas before delving into the code--be sure to be very clear about the concept of Web 2.0. When you hear the Word Web 2.0, you should first ask "What is Web 1.0?" "Although few people mention Web 1.0, it actually refers to a traditional web with a completely different request and response model. For example, click on a button on the Amazon.com website or enter a search term. A request is sent to the server, and then the response is returned to the browser. The request is not just a list of books and bibliographies, but another complete HTML page. So when a Web browser redraws with a new HTML page, you may see flicker or jitter. In fact, each new page you see can clearly see the request and response.

Web 2.0 (to a large extent) eliminates this kind of visible reciprocating interaction. Visit sites such as Google Maps or Flickr (see Resources for links to support Web 2.0 and Ajax sites). For example, on Google maps, you can drag the map, zoom in and out, and only have a few redraw operations. Of course there are still requests and responses, but they are hidden behind the scenes. As a user, the experience is more comfortable and feels like a desktop application. This new feeling and paradigm is what you experience when someone mentions Web 2.0.

What needs to be cared about is how to make these new interactions possible. Obviously, there is still a need to make requests and receive responses, but it is the HTML redraw for each request/response interaction that creates a slow, clumsy Web interaction experience. So it is clear that we need a way to make the requests sent and the responses received contain only the data needed, not the entire HTML page. The only time you need to get the entire new HTML page is when you want the user to see the new page.

Most interactions, however, add detail to an existing page, modify the subject text, or overwrite the original data. In these cases, the Ajax and Web 2.0 methods allow data to be sent and received without updating the entire HTML page. For those who often surf the web, this ability can make your application feel faster and respond more promptly, allowing them to visit your site from time to times.

XMLHttpRequest Introduction

To truly achieve this brilliant miracle, you must be very familiar with a JavaScript object, the XMLHttpRequest. This little object has actually been around for some time in several browsers, and it is the core of Web 2.0, Ajax, and most other content that this column will cover over the next few months. To give you a quick overview of it, here are a few of the few methods and properties that will be used for that object.

Open (): establishes a new request to the server.
send (): Sends a request to the server.
abort (): Exits the current request.
readystate: Provides the ready state of the current HTML.
ResponseText: The request response text returned by the server.

If you don't know these (or any of them), you don't have to worry, we'll cover each method and property in the next few articles. What should be understood now is what to do clearly with XMLHttpRequest. Note that these methods and properties are related to sending requests and handling responses. In fact, if you see all the methods and properties of XMLHttpRequest, they are all related to a very simple request/response model. Obviously, we will not encounter special new GUI objects or some kind of hyper-mystical way of creating user interaction, we'll use very simple requests and very simple responses. It doesn't sound like much of an attraction, but using that object can completely change your application.

The Simple new

You first need to create a new variable and assign it a XMLHttpRequest object instance. This is simple in JavaScript, as long as you use the new keyword for that object name, as shown in Listing 1.

Listing 1. Create a new XMLHttpRequest object

<script language= "javascript" type= "Text/javascript" >
var request = new XMLHttpRequest ();
</script>
Not hard, huh? Remember that JavaScript does not require a variable type to be specified, so it does not need to be done as in Listing 2 (which may be required in the Java language).

Listing 2. Creating Java Pseudo-code for XMLHttpRequest

XMLHttpRequest request = new XMLHttpRequest ();
So create a variable in JavaScript with Var, give it a name (like "request"), and assign it a new XMLHttpRequest instance. You can then use the object in the function.

Error handling

In fact all sorts of things can go wrong, and the above code does not provide any error handling. A better approach is to create the object and gracefully exit when a problem occurs. For example, any older browser (whether or not you believe that someone is still using the older version of Netscape Navigator) does not support XMLHttpRequest, and you need to let these users know that something is wrong. Listing 3 shows how to create the object to issue a JavaScript warning when a problem occurs.

Listing 3. Create a XMLHttpRequest with error handling capabilities


<script language= "javascript" type= "Text/javascript" >
var request = false;
try {
Request = new XMLHttpRequest ();
} catch (Failed) {
Request = false;
}

if (!request)
Alert ("Error initializing xmlhttprequest!");
</script>
Be sure to understand these steps:

1. Create a new variable request and assign a value of false. The following uses false as the criteria for the decision, which indicates that the XMLHttpRequest object has not been created yet.
2. Add Try/catch block:
1) Try to create the XMLHttpRequest object.
2) If the failure (catch (failed)) is guaranteed, the value of request is still false.
3. Check if request is still false (not false if everything is OK).
4. If a problem occurs (request is false) use a JavaScript warning to notify the user that a problem has occurred.

The code is very simple, and for most JavaScript and WEB developers it takes longer to really understand it than to read and write code.  Now that you have a XMLHttpRequest object creation code with error checking, you can also tell you what went wrong. Ajax's sub-assembly:

function Ajax (option) {
var Obj=window. Xmlhttprequest?new XMLHttpRequest (): New ActiveXObject (' microsoft.xmlhttp ');

var Url=option.url;

if (!url) {

return false;

}

var type=option.type| | ' Get ';

var data=option.data;

var datatype=option.datatype| | ' Text ';

if (data== ') {

Data=option.data;

}else if (typeof data== ' object ') {

var str= ';

for (var i in data) {

str+=i+ ' = ' +data[i]+ ' & ';

}

Data=str.splice (0,-1);

}

if (type== ' get ') {

Obj.open (' Get ', url+ '? ') +data);

Obj.send (NULL);

}else if (type== ' post ') {

Obj.open (' post ', url);

Obj.send (data);

}

Obj.onreadystatechange=function () {

if (obj.readystate==4) {

if (obj.status==200) {

var result= ';

if (datatype== ' text ') {

Result=obj.responsetext;

}else if (datatype== ' xml ') {

Result=obj.responsexml;

}else if (datatype== ' json ') {

Result=eval (' (' +obj.responsetext+ ') ');

}

Option.success (result);

}

}

}

}

Introduction to Ajax

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.