Today, in the project, I needed to load remote data and render the page. I didn’t realize the problem of ajax cross-domain request until the development stage. I vaguely remembered that Jquery mentioned a solution to ajax cross-domain request, so I immediately dug out Jquery’s The API came out for research and found that
JQuery has two types of solutions for Ajax cross-domain requests, but both only support the get method. They are JQuery's jquery.ajax jsonp format and jquery.getScript method.
What is jsonp format? API original text: If the obtained data file is stored on a remote server (with different domain names, that is, cross-domain data acquisition), you need to use the jsonp type. Using this type creates a query string parameter callback=? which is appended to the requested URL. The server should add the callback function name before the JSON data in order to complete a valid JSONP request. This means that the remote server needs to process the returned data and return a callback (json) data according to the callback parameters submitted by the client, and the client will use script to process the returned data to process the json data. deal with. JQuery.getJSON also supports the jsonp data method call.
Client-side JQuery.ajax calling code example:
Sample code for data returned by the server: