Java and Ajax cross-domain execution

Source: Internet
Author: User

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

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.