The most basic ajax interaction code: json cross-origin transfer between jquery and java, and return of java json code, jqueryjson

Source: Internet
Author: User

The most basic ajax interaction code: json cross-origin transfer between jquery and java, and return of java json code, jqueryjson

First import the jar package

The jar package above is mainly used to convert map or list data into a json string and pass it to the foreground for use.

Static Page code: 2.html

1 <! DOCTYPE html> 2 

Create a servlet and the server servlet --- ajax. java code:

The web. xml here is omitted.

Returns the list set (array)

 1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.ArrayList; 6 import java.util.List; 7  8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet;10 import javax.servlet.http.HttpServletRequest;11 import javax.servlet.http.HttpServletResponse;12 13 import net.sf.json.JSONArray;14 15 public class Ajax extends HttpServlet {16     public void doGet(HttpServletRequest request, HttpServletResponse response)17             throws ServletException, IOException {18         doPost(request, response);19     }    20     public void doPost(HttpServletRequest request, HttpServletResponse response)21             throws ServletException, IOException {        22         //response.setHeader("Access-Control-Allow-Origin", "*");23         response.setContentType("text/plain");24         response.setCharacterEncoding("utf-8");25         String jsonp=request.getParameter("callbackparam");26         String name=request.getParameter("data");27         System.out.println("param:"+name);        28         List<String> list=new ArrayList<String>();29         list.add("abc");30         list.add("dcc");31         list.add("1");32         list.add("2");33         list.add("3434");34         list.add("retr");        35         JSONArray jsonArray=JSONArray.fromObject(list);36         String result=jsonArray.toString();37         PrintWriter writer=response.getWriter();38         writer.write(jsonp+"("+result+")");39     }40 41 }

Run:

The server returns map Data (json)

Modify ajax. java code

 1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 import javax.servlet.ServletException;11 import javax.servlet.http.HttpServlet;12 import javax.servlet.http.HttpServletRequest;13 import javax.servlet.http.HttpServletResponse;14 15 import net.sf.json.JSONArray;16 import net.sf.json.JSONObject;17 18 public class Ajax extends HttpServlet {19     public void doGet(HttpServletRequest request, HttpServletResponse response)20             throws ServletException, IOException {21         doPost(request, response);22     }    23     public void doPost(HttpServletRequest request, HttpServletResponse response)24             throws ServletException, IOException {25         response.setContentType("text/plain");26         response.setCharacterEncoding("utf-8");27         String callbackparam=request.getParameter("callbackparam");28         System.out.println("callbackparam:"+callbackparam);29         String name=request.getParameter("data");30         System.out.println("param:"+name);    31         Map<String, String> map=new HashMap<String, String>();32         map.put("1", "a");33         map.put("2", "b");34         map.put("3", "c");35         map.put("4", "d");        36         JSONObject jsonObject=JSONObject.fromObject(map);37         String result=jsonObject.toString();        38         PrintWriter writer=response.getWriter();39         writer.write(callbackparam+"("+result+")");40     }41 42 }

Modify the Page code:

1 <! DOCTYPE html> 2 

Run it and see:

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.