Use of Ajax in Javascript

Source: Internet
Author: User
Tags ajax status code

Ajax is "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML). The core of AJAX is the JavaScript Object XMLHttpRequest. This object was first introduced in Internet Explorer 5. It is a technology that supports asynchronous requests. In short, XMLHttpRequest allows you to use JavaScript to request and process responses to the server without blocking users.

Ajax advantages:

Ajax is not a new programming language, but a technology used to create better, faster, and more interactive web applications. With this object, your javascript can exchange data with the Web server without reloading the page. Ajax uses asynchronous data transmission (HTTP request) between the browser and the web server, so that the webpage can request a small amount of information from the server, rather than the whole page. AJAX can make Internet applications smaller, faster, and more friendly. Ajax is a browser technology independent of web server software.

Ajax disadvantages:

It will damage the normal behavior of the browser's back button. When a page is dynamically updated, the user cannot return to the status of the previous page, because the browser can only write down the static page in the history.

Ajax case:

Google uses its googlesuggest to make Ajax popular. Google suggest uses ajax to create a dynamic web interface: When you enter keywords in the Google search box, JavaScript will send these characters to the server, and then the server will return a list of search recommendations.

Ajax status value and status code

The Ajax status value refers to several States that have been used to run Ajax. no matter whether the access is successful or not, the response steps can be understood as Ajax running steps. Use Ajax. readystate. (From numbers 1 ~ 4 units of digits ). Ajax status code indicates the HTTP header information code returned by the server based on the information submitted by the HTTP protocol, regardless of whether Ajax access is successful or not. status obtained. This is why we use the following method to determine whether the obtained information is correct when using Ajax.
If (Ajax. readystate = 4 & Ajax. Status = 200 ){.....}

Ajax running steps and Status values (most of us use "4" for judgment)
0-(not initialized) The send () method has not been called
1-(load) The send () method has been called and a request is being sent.
2-(Load completed) The send () method is executed completely,
3-(interaction) parse the response content
4-(complete) The response content has been parsed and can be called on the client
Ajax Status Code Description (list the most common ones)
200 -- transaction successful
404 -- no file, query, or URL found
500 -- Internal error occurred on the server
505 -- the server does not support or reject the specified HTTP version in the Request Header

Create an Ajax object

<script type="text/javascript">function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("GET","http://localhost:8887/jq/css/lamf.css",true);xmlhttp.send();}</script>

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.