JSONP cross-domain data calls

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.