(JS/JQ) and Ajax

Source: Internet
Author: User

JS with Ajax (asynchronous JS and XML):

Common methods for 1.XMLHttpRequest objects:

Open () Prepare request send () Send request abort () Cancel Request

ReadyState (Request status Code): 0 (not started), 1 (ON), 2 (Delivered), 3 (Received), 4 (loaded)

Status (HTTP request state code): 404 (File Not found), (OK)

onReadyStateChange: A function reference is called when the request state changes

ResponseText: Plain text string returned by the server

Responsexml: XML Format object returned by the server

2. Create a Request object function Createrequest ()

function Createrequest () {  try {    request = new XMLHttpRequest ();  } catch (tryms) {    try {      request = new ActiveXObject ("Msxml2.xmlhttp");    } catch (otherms) {      try {        request = new ActiveXObject ("Microsoft.XMLHTTP");  For IE browser
} catch (failed) { request = null;}} } return request;}

Get requested object: Request = Createrequest ();

Set request path and request data: var url = "register.php";
var requestData = "Username=" + Escape (document.getElementById ("username"). value); POST request

Configuration request: Request.open ("Get/post", Url,true (asynchronous)/false (synchronous))

Configuration callback function: Request.onreadystatechange = displaydetails;

Set Request Header: Request.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); POST request

Send request: Request.send (NULL); GET request

Request.send (RequestData); POST request

To create a callback function: function Displaydetails ()

Cases:

function Displaydetails () {  if (request.readystate = = 4) | | (Request.status = = 200)) {      Detaildiv = document.getElementById ("description");      detaildiv.innerhtml = Request.responsetext;  }}

JQ and Ajax:

1.$.get (URL,callback);

Example: $. Get("url", function (data) {alert ("Data")});

2.$.post (URL,data,callback);

Example: $. post ("url", {max: ' jq', Name: ' Eezz' }, function (data) {alert ("data") });

3.$.ajax

Jquery.ajax ({                type: "POST",                url: "url",                data: "Max=jq &name=eezz",
DataType: "JSON",
Success:function (msg) {alert (msg);}}); $.ajax Method Parameters: http://blog.sina.com.cn/s/blog_4f925fc30100la36.html

(JS/JQ) and 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.