欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

json格式的Ajax提交示例代码

程序员文章站 2023-11-14 20:09:22
需要加入:com.springsource.org.apache.commons.logging-1.1.1.jar、org.json.jar和jquery-1.10.0....

需要加入:com.springsource.org.apache.commons.logging-1.1.1.jar、org.json.jar和jquery-1.10.0.min.js包

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

<script type="text/javascript"> 
//扫描二维码 
$('#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+')');//拼接的json串 

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.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(); 
} 
}