Sample Code for json Ajax submission, sample code for jsonajax
Need to add: com.springsource.org. apache. commons. logging-1.1.1.jar, org. json. jar and jquery-1.10.0.min.js package
<Form class = "form-inline"> scan code: <input id = "txtQRCode" type = "text"> <button id = "btnReceive"> OK </button> </form> <script type = "text/javascript"> // scan the QR code $ ('# btnReceive '). click (function () {$. ajax ({type: "GET", url: "<% = basePath %> asynchronous/receive. do ", data: {qrCode: $ ('# txtqrcode '). val ()}, dataType: "json", cache: false, success: function (msg) {// var json = eval ('+ msg + ')'); // The spliced json string var flag = msg. flag; Var info = msg.info; if (flag) {alert (info); $ ('# txtQRCode '). val ("") ;}else {alert (info) ;}, error: function (error) {alert (error );}});});}); </script> package com. utcsoft. pcapps. selfservice. controller; import java. io. IOException; import java. io. printWriter; import java.net. unknownHostException; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import Org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import org. json. JSONException; import org. json. JSONObject; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import com. utcsoft. common. attributes. yesNoEnum; import com. utcsoft. common. util. dateOperate; import com. utcsoft. pcapps. selfservice. attributes. keyReque StAttr; import com. utcsoft. pcapps. selfservice. dao. flowDao; import com. utcsoft. pcapps. selfservice. dao. keyRequestDao; import com. utcsoft. pcapps. selfservice. entity. flow; import com. utcsoft. pcapps. selfservice. entity. keyRequest; import com. utcsoft. pcapps. selfservice. entity. utcUsers; @ Controller @ RequestMapping (value = "/asynchronous") public class AsynchronousController {private final static Log logge R = LogFactory. getLog (AsynchronousController. class ); /*** scan the QR code to change the order to UTC review * @ param request * @ param response * @ throws IOException * @ throws JSONException */@ RequestMapping (value = "/receive ") public void receive (HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {String rets = ""; boolean B = false; try {String qrcode = request. getParameter ("qrCode") = null? "": Request. getParameter ("qrCode"). toString (). trim (); logger.info (qrcode); if (! "". Equals (qrcode) {JSONObject obj = new JSONObject (qrcode); String orderid = obj. getString ("orderid"). trim (); if (! "". Equals (orderid) {KeyRequestDao keyDao = new KeyRequestDao (); KeyRequest k = new KeyRequest (); // The overall information k = keyDao. findOneByOrderId (orderid); if (k! = Null) {// modify the order status to audit int n = keyDao. updateState (orderid, String. valueOf (KeyRequestAttr. kqStateEnum. verify. getStep (); Flow flow = new Flow (); FlowDao flowDao = new FlowDao (); UtcUsers users = (UtcUsers) request. getSession (). getAttribute ("utcUsers"); // insert records into the Flwo table flow. setKq_id (orderid); flow. setKq_state (String. valueOf (KeyRequestAttr. kqStateEnum. cus_send.getStep (); flow. setKq_state_to (String. valueOf (KeyRequestAttr. kqStateEnum. verify. getStep (); flow. setIs_Pass (YesNoEnum. y. getCode (); flow. setUser_id (users. getUser_id (); flow. setUser_id_from (users. getUser_id (); flow. setOpTimer (DateOperate. getDateTime (); int flowNum = flowDao. save (flow); logger.info ("Scan QR code submission result: n =" + n + "; flowNum =" + flowNum); if (n> 0) {B = true; rets = "UTC received order succeeded"; logger.info ("UTC received order succeeded");} else {rets = "UTC failed to receive order "; logger.info ("failed to receive order in UTC") ;}} else {rets = "failed to query order"; logger.info ("failed to query order ");}} else {rets = "the order number in the QR code is blank"; logger. error ("Empty Order Number In QR code");} else {rets = "Empty QR code content"; logger. error ("Empty QR code content");} catch (Exception e) {e. printStackTrace (); rets = e. getMessage (); logger. error (e. getMessage ();} JSONObject ret = new JSONObject (); ret. put ("flag", B); ret. put ("info", rets); PrintWriter write = response. getWriter (); write. write (ret. toString (); write. flush ();}}
Can json be submitted only through ajax? Can I place json data in a value of a form and submit it with other form items?
Ajax is only one way. Of course, you can submit the json file together with other places (such as hidden fields. And the effect is still one type. You can use json_decode in the background.
Ask the front-end how to use ajax to submit. The format is json submitted to the back end and then parsed in the background. This is a small example?
Leave a number for you. For a provincial/municipal contact, return in json format.