Springmvc front-back and back-desk interaction

Source: Internet
Author: User

The JSON object is returned in the background:

Package Com.sawshaw.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import com.alibaba.fastjson.jsonobject;@ Controller@requestmapping ("/hello") public class Hellocontroller {@RequestMapping ("/greeting") @ResponseBodypublic String greeting () {jsonobject js=new jsonobject (); Js.put ("id", "myId"); Js.put ("Content", "mycontent"); return Js.tojsonstring ();}}

Front Office Analysis:

$ (document). Ready (function () {            $.ajax ({                URL: ' hello/greeting '            }). Then (function (data) {            var obj= Json.parse (data);             or Var obj=eval ("(" +data+ ")"); analytic            console.log (obj.id);               $ ('. Greeting-id '). Append (obj.id);               $ ('. Greeting-content '). Append (obj.content);            })        ;        </script>

Returns the array data in the background: 

@Overridepublic void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexception {int user_id = Integer.parseint (Request.getparameter ("user_id")); User User;try {user = Service.queryuserbyid (user_id), if (user! = null) {Response.getwriter (). Print (User.getuser_name () + "," + user.getuser_phone () + "," + user.getuser_email () + "," + user.getuser_adress ());} else {response.getwriter (). Print ("false");}} catch (SQLException e) {e.printstacktrace ();}}

Front Office Analysis:

<script>$ (function () {   var id=geturlparam ("id"). Replace (/\+/g, "");   $ ("#u_id"). Val (id);   $.ajax ({url: ' Querybyuserid ', type: ' Get ', ContentType: ' text/html ', data: {User_id:id}}). Done (function (data) {if (data !=false) {var arr=data.split (","), if (arr[0]!= "null") {$ ("#u_name"). Val (arr[0]);} if (arr[1]!= "null") {$ ("#u_phone"). Val (arr[1]);} if (arr[2]!= "null") {$ ("#u_email"). Val (arr[2]);} if (arr[3]!= "null") {$ ("#u_adress"). Val (arr[3]);}} Else{alert ("Find user Failed");}}). Fail (function (data) {}). Always (function () {Console.log (' complete ');}); $ ("#updateSubmit"). Click (function () {if ($ ("#u_name"). val () = = "") {alert ("User name is not empty");});}   );   function Geturlparam (name) {var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");//Constructs a regular expression object containing the target parameter var r = WINDOW.LOCATION.SEARCH.SUBSTR (1). Match (reg);  Match target parameter if (r!=null) return unescape (r[2]); return null; Return parameter value}   </script>

The background returns the JSON array:

@WebServlet (urlpatterns = "/userqueryorder") public class Orderuserqueryorderservlet extends HttpServlet {private Static final Long Serialversionuid = 1l;orderservice Service = new Orderserviceimpl (), @Overridepublic void Doget (Httpserv Letrequest request, HttpServletResponse response) throws Servletexception, IOException {HttpSession session = Request.getsession (); User user = (user) Session.getattribute ("user"); int user_id = user.getuser_id (); try {list<order> List = Service.use Rqueryorder (USER_ID); Jsonarray orderlist = jsonarray.fromobject (list); Response.getwriter (). println (orderlist);} catch (Exception e) {e.printstacktrace ();}}

  

Front Office Analysis:

$.ajax ({url: ' Userqueryorder ', type: ' Get ', DataType: ' JSON ', ContentType: ' text/html '}). Done (function (data) {//var Json=eval ("(" +data+ ")"); for (Var i=0;i<data.length;i++) {$ ("#orderList"). Append ("<table>" + "<tr class=" Order_Detail ' > "+" <td class= ' order_id ' > "+ data[i].order_id+" </td> "+" <td class= ' Order_price ' > " +data[i].order_price+ "</td>" + "<td class= ' order_time ' >" + (parseint (data[i].order_time.year) +1900) + "-" + (parseint (data[i].order_time.month) +1) + "-" + (Data[i].order_time.date) + "" + (parseint (data[i].order_time.hours) +1 + ":" + (Data[i].order_time.minutes) + ":" + (Data[i].order_time.seconds) + "</td>" + "</tr>" + "</table > "+" <table class= ' OrderDetail ' style= ' display:none; ' > "+" <tr> "+" <td> Product name </td> "+" <td> price (Yuan) </td> "+" <td> quantity </td> "+" </tr > "+" </table> "+" <div class= ' XX ' > "+" </div> ");}}). Fail (function (data) {

  

 

 

  

Springmvc front-back and back-desk interaction

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.