Ajax get and post requests

Source: Internet
Author: User
<Script language = "JavaScript"> var xmlhttpreq; // create the XMLHTTPRequest object function createxmlhttprequest () {If (window. XMLHttpRequest) {// Mozilla xmlhttpreq = new XMLHttpRequest ();} else if (window. activexobject) {// IE browser try {xmlhttpreq = new activexobject ("msxml2.xmlhttp");} catch (e) {try {xmlhttpreq = new activexobject ("Microsoft. XMLHTTP ") ;}catch (e) {}}// send the request function sendrequest (URL) {createxmlhttprequest (); xmlhttpreq. open ("get", URL); xmlhttpreq. onreadystatechange = processresponse; // specify the response function xmlhttpreq. send (null); // send request} // function for processing returned information function processresponse () {If (xmlhttpreq. readystate = 4) {// judge the object state if (xmlhttpreq. status = 200) {// information returned successfully. Start to process information var res = xmlhttpreq. responsetext; window. alert (RES);} else {// The page is abnormal window. alert ("the page you requested has an exception. ") ;}}Ii. Send the POST request var xmlhttpreq; // create the XMLHTTPRequest object function createxmlhttprequest () {If (window. XMLHttpRequest) {// Mozilla xmlhttpreq = new XMLHttpRequest ();} else if (window. activexobject) {// IE browser try {xmlhttpreq = new activexobject ("msxml2.xmlhttp");} catch (e) {try {xmlhttpreq = new activexobject ("Microsoft. XMLHTTP ") ;}catch (e) {}}// send the request function sendrequest (URL, para) {Createxmlhttprequest (); xmlhttpreq. open ("Post", URL, true); xmlhttpreq. onreadystatechange = processresponse; // specify the response function xmlhttpreq. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); xmlhttpreq. send (para); // send request} // function of processing returned information function processresponse () {If (xmlhttpreq. readystate = 4) {// judge the object state if (xmlhttpreq. status = 200) {// information returned successfully. Start to process information var res = xmlhttpreq. responsetext; Window. Alert (RES);} else {// The page is abnormal. Window. Alert ("the page you requested has an exception. ");}}}

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.