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: