XMLHttpRequest and Webapi in JavaScript

Source: Internet
Author: User

XMLHttpRequest and Webapi in JavaScript it's just a client-to-server relationship. The XMLHttpRequest in JavaScript is responsible for initiating requests on the client side, and the Web API is responsible for returning the response on the server.

Web API:

Unlike people, the code does not have a pair of eyes that can read or look at a picture. It can only view "something" in a format that it can read. This is where the data Interchange format (such as JSON) comes in. Web APIs are a set of directives and standards that interact with HTTP services. These interactions can include actions such as Create, read, update, delete, and the Web API will have a description that outlines how to use these directives and standards.

These operations that JavaScript does behind the scenes, such as requesting an item of data, are called asynchronous operations. Asynchronous operations typically refer to operations that occur behind the scenes and do not disrupt the main process.

The XMLHttpRequest object in javascript:

JavaScript is an object-based language, and XMLHttpRequest is a class of objects. When you use the new XMLHttpRequest () syntax and return a value to a variable, it has the ability to request resources from an address.

We need to look at the following available functions in XMLHttpRequest:

1, open (Method,url,async (optional), user (optional), password (optional));

2, Send ().

And the following properties:

1, Onreadstatechange (can be assigned to it in code as a function);

2, ReadyState (return a 0~4) value, used to indicate the status code;

3, status (return HTTP status code, such as 200 to indicate the success of the request);

4, ResponseText (when the request succeeds, the property contains the response body as text, such as the JSON we requested).

Note: The value of the property can be a function. Because functions in JavaScript are also a class of objects. An object is a class of data, so it can be assigned to a variable (property), modified, and passed. In programming, this situation is called "function is a class citizen". The value of onreadystatechange should be a function.

Create a XMLHttpRequest object and let it get JSON data from the Openweather-map API:

In the second line of code in this example, a string is created that holds the URL of the JSON resource. A function is then assigned to the onReadyStateChange property of the Myxmlhttprequest. The function is executed each time the ReadyState property changes. In this function, the readystate value is determined to be 4 (for completion), and the HTTP status code is 200 (indicates success). If both conditions return true, the JSON text is parsed into a JSON object.

XMLHttpRequest and Webapi in JavaScript

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.