I have seen of JSONP

  • 2020-05-17 04:48:58
  • OfStack

JavaScript beginners, 1 point of their own understanding, there are inappropriate please correct ~

JSONP:

The method used by the local html file to obtain data from the xxx website (across domains).

Cross-domain issues:

Data from the external server can only be accessed by js from the external server, and local js cannot be accessed by XMLHttpRequest().

Web services provide JSON data in external servers, such as in http: / / gumball wickedlysmart. com /, the data included in the function updateSales as a parameter, is now in the local html access external JSON data in the server?

1. Check the Web service document to specify the parameter names actually used by the service (only one parameter in this example is an array of objects);
2, a callback function, specified in the url http: / / gumball wickedlysmart. com / & # 63; callback=updateSales, which specifies the name of the function to access JSON data in the Web service;
3. Create the function updateSales(object) in the local js file with the function name just defined, and write the method to process the external JSON data;
4, in the html file < body > Use under label < script > The tag links to the external Web service, and url is the path in 2.

Conclusion:

The purpose of JSONP is to provide a local html1 function interface (local html can be added via url). In order to ensure the security of data access, the server places the secure data in the parameters of the callback function. The internal parameters of the function are the data provided by the server to the local. As for how to use the data, the callback function is defined in the local js.

Note: local requests for access to the Web service using JSONP may obtain insecure Js code, so be sure to trust the Web service.

JSONP and XMLHttpRequest:

XMLHttpRequest is used for the development of internal Web services, internal access to internal, the same domain access will be easier.

JSONP is used when accessing external data (cross-domain access).

The above is the content of this article, hope you can enjoy it, can help you learn jsonp.


Related articles: