Home >Web Front-end >JS Tutorial >Jsonp cross-domain principle and Jquery solution_javascript skills

Jsonp cross-domain principle and Jquery solution_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:15921browse

Principle: 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.
Personal understanding:

is to dynamically register a function a(data) on the client, and then pass the function name to the server, and the server returns a({/*json*/}) to the client Run, thus calling the client's function a(data), thus achieving cross-domain.

Copy code The code is as follows:


< ;html xmlns="http://www.worg/xhtml" >

Test Jsonp








jQuery solution:
Copy code The code is as follows:




Test Jsonp







jquery’s jsoncallback is dynamically generated, and the actual request server address is: http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=jsonp1274058545738
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn