ajax and jsonp are both request methods, so what is the difference between ajax and jsonp? This article will introduce to you the difference between ajax and jsonp. Friends who are interested can take a look.
First of all, we should know that ajax and jsonp can communicate with the background to obtain data and information, but do not need to refresh the entire page to achieve partial refresh of the page.
Next let’s take a look at the difference between ajax and jsonp.
The difference between ajax and jsonp:
ajax is a technology that sends http requests for asynchronous communication with the background. The principle is to instantiate the xmlhttp object and use this object to communicate with the background.
A complete AJAX request generally includes the following steps:
(1) Instantiate the XMLHttpRequest object
(2) Connect to the server
(3) Send Request
(4) Receive response data
jsonp is a data communication format that can send http requests across domains and can be embedded in ajax. The principle is to use the script tag to link resources across domains.
JSONP consists of two parts: callback function and data. The callback function is generally controlled in the browser and sent to the server as a parameter (of course, you can also fix the name of the callback function, but the difference between the client and server The names must be consistent). When the server responds, the server will combine the function and data into a string and return it.
The request process of JSONP is as follows:
Request phase: The browser creates a script tag and assigns a value to its src.
Send a request: When assigning a value to the src of the script, the browser will initiate a request.
Data response: The server concatenates the data to be returned as parameters and the function name (the format is similar to "jsonpCallback({name: 'abc'})") returns. When the browser receives the response data, since the request is initiated by script, it is equivalent to directly calling jsonpCallback method, and a parameter is passed in.
Finally: jsonp only supports get requests, and ajax supports get and post requests.
The above is the detailed content of What are the differences between ajax and jsonp? Introduction to the differences between ajax and jsonp. For more information, please follow other related articles on the PHP Chinese website!