Citation: http://kb.cnblogs.com/page/139725/
When invoking a JS file on a Web page, it is not affected by cross-domain (not only that, we also find that all tags that have the attribute "src" have cross-domain capabilities, such as <script>, , <iframe>)
<%@ Page Language="Java"ContentType="text/html; charset=gb18030"pageencoding="GB18030"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=gb18030"><title>Insert Title here</title></Head><Scripttype= "Text/javascript"src= "Jquery-1.9.1.js"></Script><Script> functionHandledata (data) {alert (json.stringify (data)); } functionLoadData () {varURL= "/myweb/testjsonp?code=ca1998&callback=handledata"; Cross-domain URL//provide the URL address of the JSONP service (regardless of the type of address, the resulting return value is a piece of JavaScript code) //Create a script tag, set its properties varScript=Document.createelement ('Script'); Script.setattribute ('src', URL); //Add the script tag to head, and the call startsdocument.getElementsByTagName ('Head')[0].appendchild (script); } functionLoadajax () {varURL= "/myweb/testjsonp?code=ca1998";//cross-domain URL $.ajax ({type:"Get", //JSONP type can only use GET, cannot use post, here does not write default to getAsync:false, Url:url, DataType:"Jsonp", Jsonp:"Callback",//The parameter name that is passed to the request handler or page to obtain the name of the JSONP callback function (generally by default: callback) //jsonpcallback: "Handledata",//Custom JSONP callback function name, default to jquery automatically generated random function name, can also write "?", jquery will automatically process the data for youSuccess:function(JSON) {handledata (JSON); Alert ('you find flight information'); }, Error:function() {alert ('fail'); } }); }</Script><Bodyonload= "Loadajax ()"></Body></HTML>
PackageBookstore;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class TESTJSONP*/@WebServlet ("/testjsonp") Public classTestjsonpextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /** * @seeHttpservlet#httpservlet ()*/ PublicTestjsonp () {Super(); //TODO auto-generated Constructor stub } /** * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/ protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubDoPost (Request,response); } /** * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/ protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubString code = request.getparameter ("code"); String Callback= Request.getparameter ("Callback"); Response.getwriter (). Write (Callback+ "({code: '" +code+ "})"); }}
JSONP cross-domain data calls