Home >Web Front-end >JS Tutorial >js/ajax spanning access-jsonp principles and examples (javascript and jquery implementation code)_javascript skills
Fortunately, I saw the rising sun again after the apocalypse, so I can still write articles here. Let’s get back to the topic. I recently worked on a project and needed to use a subdomain name to call an existing function under the main domain name, so I thought of it. Use jsonp to solve it. There are many friends who have this need in our usual projects, so we recorded it for future reference and hope it can help everyone.
What is the JSONP protocol?
JSONP is JSON with Padding. Due to the restrictions of the same-origin policy, XmlHttpRequest is only allowed to request resources from the current source (domain name, protocol, port). If we want to make a cross-domain request, we can make a cross-domain request by using the script tag of html and return the script code to be executed in the response, where the javascript object can be passed directly using JSON. This cross-domain communication method is called JSONP.
Obviously, JSONP is a Script Injection behavior and requires special attention to its security.
jsonp instance in Jquery
We need two pages to assume the client and server roles of the protocol respectively.
Client code: