Using JavaScript to implement various cross-origin method_basic knowledge

Source: Internet
Author: User
This article introduces how to implement various cross-origin methods in JavaScript. For more information, see I. Concepts

① Traditional Ajax: Interactive Data Format-custom string or XML description;

Cross-origin: solve the problem through the server proxy.

② Optimal solution today: Use JSON format for data transmission and JSONP for cross-origin.

③ JSON: a data exchange format. Based on plain text and supported by native JS.

Format: Two Data Type Descriptors: braces {} and square brackets []. Delimiter comma, ing character colon, and definition character.

④ JSONP: An unofficial cross-domain data interaction protocol.

  1,JavaScript files can be called on Web pages across domains. Extension: All tags with src attributes have cross-domain capabilities.

  2,The cross-origin server dynamically generates data and saves it to js files (usually with a json suffix) for the client to call.

  3,To facilitate the client's use of data, an informal transmission protocol is formed, called JSONP. The focus of this Protocol is to allow users to pass a callback parameter to the server. When the server returns data, the callback parameter is wrapped in JSON data as the function name, this allows the client to customize its own functions to automatically process returned data.


Ii. JSONP implementation

Instance 1 -- the client receives the following message:

① Client -- create a function object in the client settings. The name can be callFunc, which is used to receive and process the js data of the server.
The core of js data is that the callFunc function is called with a parameter, which is the value of the data object.

The Code is as follows:


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.