Home > Web Front-end > JS Tutorial > body text

Ajax submission sample code in json format

韦小宝
Release: 2017-12-31 10:18:48
Original
1423 people have browsed it

This article mainly introduces Ajax submission in json format. It is necessary to introduce some jar packages and js packages. jsThe sample code is as follows. Feelings about js Interested friends can come and take a look!

Need to add: com.springsource.org.apache.commons.logging-1.1.1.jar, org.json.jar and jquery-1.10.0.min.js packages

<form class="form-inline"> 
扫码: <input id="txtQRCode" type="text"> 
<button id="btnReceive">确定</button> 
</form> 

<script type="text/javascript"> 
//扫描二维码 
$(&#39;#btnReceive&#39;).click(function(){ 
$.ajax({ 
type:"GET", 
url:"<%=basePath%>asynchronous/receive.do", 
data:{qrCode:$(&#39;#txtQRCode&#39;).val()}, 
dataType:"json", 
cache:false, 
success:function(msg){ 
//var json = eval(&#39;(&#39;+msg+&#39;)&#39;);//拼接的json串 

var flag = msg.flag; 
var info = msg.info; 
if(flag){ 
alert(info); 
$(&#39;#txtQRCode&#39;).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.KeyRequestAttr; 
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 logger = LogFactory.getLog(AsynchronousController.class); 

/** 
* 扫描二维码 将订单改为UTC审核 
* @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();//综合信息 
k = keyDao.findOneByOrderId(orderid); 
if(k!=null){ 
//修改订单状态为审核 
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"); 
//将记录插入Flwo表中 
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("扫描二维码提交结果:n="+n+";flowNum="+flowNum); 
if(n>0){ 
b = true; 
rets = "UTC接收订单成功"; 
logger.info("UTC接收订单成功"); 
} 
else{ 
rets = "UTC接收订单失败"; 
logger.info("UTC接收订单失败"); 
} 
} 
else{ 
rets = "订单查询失败"; 
logger.info("订单查询失败"); 
} 
} 
else{ 
rets = "二维码中订单号为空"; 
logger.error("二维码中订单号为空"); 
} 
} 
else{ 
rets = "二维码内容为空"; 
logger.error("二维码内容为空"); 
} 

} 
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(); 
} 
}
Copy after login


The above is all the code of this article, I hope it will be helpful to everyone! !

Related recommendations:

jquery ajax solution to the problem of not refreshing when data is requested multiple times

ajax Example of registration information verification without refreshing

ajax processing three data types returned by the server


The above is the detailed content of Ajax submission sample code in json format. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!