Problem Source: A JSP page has a link that requires access to a third-party interface, and the third party returns the data to me.
Problem thinking: The first thing to think about is to use the Ajax asynchronous way to the third-party interface, the third-party interface returns a series of parameters, and according to the success parameter, on the page frame display.
Process parsing: Page Write full ajax return JSON data discovery no matter what kind of execution error, after a series of lookups to find the problem is Ajax cross-domain to get data only with Jsonp, (sad, see the API to understand what is going on), paste the code:
JSP page:
<%@ page Language= "java" import= "java.util.*" pageencoding= "GBK"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><script type= "Text/javascript" src= "common/jquery-1.6.min.js" ></script><script type= "text/ JavaScript > function Insert () {$.ajax ({type: "GET", url: "Http://10.40.114.59:8090/test/test.action", DataType: "Jsonp", Jsonp: "Callbackparam", Jsonpcallback: "Success_jsoncallback", Success:function (JSON) {alert (JSON) ; Alert ("Success");},error:function () {alert ("error");}}); </script> Servlet Code:
<servlet> <servlet-name>test</servlet-name> <servlet-class>test. receiveformaction</servlet-class> </servlet> <servlet-mapping> <servlet-name >test</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
Java code:
Package Test;import Java.io.ioexception;import Java.io.printwriter;import java.util.hashmap;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.json.jsonexception;import Org.json.jsonobject;public Class Receiveformaction extends Httpservlet{public void doget (httpservletrequest request,httpservletresponse response) {try {PrintWriter out = Response.getwriter (); String callbackname = Request.getparameter ("Callbackparam"); hashmap<string,string> hmap = new hashmap<string,string> () hmap.put ("Success", "true"); Out.write ( Callbackname+ "(" +jsonobject.valuetostring (Hmap). toString () + ")");} catch (IOException e) {e.printstacktrace ();} catch (Jsonexception e) {e.printstacktrace ()}}}
Grab Bag:
Return Data:
Java and Ajax cross-domain execution