Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between jsonp and ajax

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "what are the differences between jsonp and ajax", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the difference between jsonp and ajax" article.

Differences: 1, the core of ajax is to obtain non-page content through xmlHttpRequest, while the core of jsonp is to dynamically add script tags to call the js script provided by the server; 2. Jsonp only supports get requests, while ajax supports get and post requests.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Ajax and jsonp are called in the same way, for the same purpose, to request url and then process the data returned by the server, so frameworks such as jquery and ext encapsulate jsonp as a form of ajax. Let's take a look at jsonp and ajax and introduce the differences between them.

I. the working principle of Ajax

It is equivalent to adding an intermediate layer (AJAX engine) between the user and the server to asynchronize the user operation and the server response. For user requests, the ajax engine will do some data verification and data processing. Not all requests are submitted to the server. When new data needs to be read from the server, the Ajax engine submits the request to the server on its behalf. The biggest advantage of AJAX is that it communicates with the server to maintain data without refreshing the entire page.

Process:

Step 1: create an ajax engine object, IE6 is new ActiveXObject, and other browsers are new, a xmlHttpRequest object

Step 2: call the open method to start a request for sending. The open method passes in three parameters: the request type, the request url, and a Boolean value

Step 3: call the send method to send

Step 4: process the callback function onreadystatechange and process the response data when readState = 4 (response data completed) and status=200 (request successful).

Note: the callback function is written before open () and send ().

Second, the working principle of Jsonp

Dynamically create a script tag, using the script tag src attribute access without restrictions to achieve cross-domain.

The web client invokes the js format file (suffix .json) dynamically generated on the cross-domain server in the same way as the calling script, and the server dynamically generates the json file in order to load the data needed by the client.

Allow the user to pass a callback parameter to the server, and then when the server returns the data, it will wrap the json data with the callback parameter as the function name, so that the client can customize its own function to process the returned data automatically.

III. The difference between Ajax and jsonp

The core of ajax is to obtain non-page content through xmlHttpRequest.

The core of jsonp is to dynamically add script tags to invoke the js script provided by the server (suffix .json).

Jsonp is a way or non-mandatory protocol, and ajax does not have to use json format to transmit data.

Jsonp only supports get requests, while ajax supports get and post requests.

The above is about the content of this article on "what is the difference between jsonp and ajax". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 268

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report