HTTP request encapsulation: Ajax, RESTful APIs, and promise

Source: Internet
Author: User

One, HTTP request

HTTP is a Hypertext Transfer protocol for the network transmission of HTML files, image files, query results, and so on.

    • A complete HTTP request includes: Request line, request header, blank line, and request data (the request data can be empty)
    • HTTP request methods are: GET, POST, PUT, HEAD, DELETE, CONNECT, TRACE, OPTIONS

The request line and the request header contain information such as URL, request Method, Accept, Cookie, User agent, and so on. In practice, the client makes a full HTTP request, even if only one URL is identified. The server also returns an HTTP response (Response) that contains the response body and information such as status Code, Content Type, date, and so on. Where status code is common:

    • 200 Successful Request
    • 400 syntax error for client request, server does not understand
    • 401 Permissions error, identity authentication failed
    • 404 The Requested resource (Web page, etc.) does not exist
    • 500 Internal Server error

Second, Ajax technology

The original Web page development method is the front and back end in one, the code is written together, I request an address, the server is in the form of the corresponding body returned to me a page. The advent of Ajax has changed this, and we can take this to get the specific data we need to update the data without overloading the entire page.

var xmlhttp;if (window. XMLHttpRequest) {    xmlhttp = new XMLHttpRequest ();//ie7+, Firefox, Chrome, Opera, Safari Browser execute code}else {    xmlhttp = New ActiveXObject ("Microsoft.XMLHTTP");//IE6, IE5 browser execution Code}xmlhttp.onreadystatechange = function () {    if ( Xmlhttp.readystate = = 4 && xmlhttp.status = = $) {        document.getElementById ("mydiv"). InnerHTML = Xmlhttp.responsetext;}    } Xmlhttp.open ("GET", "/try/ajax/ajax_info.txt", true); Xmlhttp.setrequestheader ("Content-type", "application/ X-www-form-urlencoded "); Xmlhttp.send ();

As the example above shows, the most important thing in Ajax is the XMLHttpRequest object, which defines the HTTP request in several ways, processing the HTTP response:

HTTP request encapsulation: Ajax, RESTful APIs, and promise

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.