json格式的Ajax提交示例代碼
需要加入: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){
//修改訂單狀態(tài)為審核
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("掃描二維碼提交結(jié)果: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 = "二維碼中訂單號(hào)為空";
logger.error("二維碼中訂單號(hào)為空");
}
}
else{
rets = "二維碼內(nèi)容為空";
logger.error("二維碼內(nèi)容為空");
}
}
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();
}
}
- SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)
- django通過(guò)ajax發(fā)起請(qǐng)求返回JSON格式數(shù)據(jù)的方法
- jQuery中使用Ajax獲取JSON格式數(shù)據(jù)示例代碼
- 如何將ajax請(qǐng)求返回的Json格式數(shù)據(jù)循環(huán)輸出成table形式
- jsp中利用jquery+ajax在前后臺(tái)之間傳遞json格式參數(shù)
- Jquery Ajax 學(xué)習(xí)實(shí)例2 向頁(yè)面發(fā)出請(qǐng)求 返回JSon格式數(shù)據(jù)
- Ajax與用戶交互的JSON數(shù)據(jù)存儲(chǔ)格式
相關(guān)文章
jQuery Ajax的readyState和status的區(qū)別和使用詳解
在前幾篇分析了jquery的ajax異步和同步,以及異常的一些處理,感覺(jué)還沒(méi)有把a(bǔ)jax的readyState和status說(shuō)清楚.今天就來(lái)說(shuō)說(shuō)ajax狀態(tài)的那點(diǎn)事,非常不錯(cuò),對(duì)ajax readystate和status區(qū)別和使用感興趣的朋友一起學(xué)習(xí)吧2017-03-03
AJAX +SpringMVC 實(shí)現(xiàn)bootstrap模態(tài)框的分頁(yè)查詢功能
這篇文章主要介紹了AJAX +SpringMVC 實(shí)現(xiàn)bootstrap模態(tài)框的分頁(yè)查詢功能,以及模態(tài)框 ajax分頁(yè)實(shí)例代碼分享,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
基于h5 ajax實(shí)現(xiàn)手機(jī)定位(demo)
最近沒(méi)有項(xiàng)目做,正有朋友請(qǐng)幫忙實(shí)現(xiàn)手機(jī)定位功能,實(shí)現(xiàn)方法有很多種,我是用h5和ajax實(shí)現(xiàn)的手機(jī)定位,只是個(gè)demo,后續(xù)還要繼續(xù)完善的,在此分享給大家,需要的朋友可以參考下2015-09-09
ajax的responseText亂碼的問(wèn)題的解決方法
javascript的編碼是和myeclipse一樣的為什么還會(huì)出現(xiàn)問(wèn)題,下面為大家介紹下ajax的responseText亂碼的問(wèn)題的解決方法2014-05-05
Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)表格中的信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
ajax 自動(dòng)完成下拉框 自動(dòng)提示位置問(wèn)題
ajax 自動(dòng)完成下拉框 自動(dòng)提示位置問(wèn)題...2007-02-02
javascript Ajax獲取遠(yuǎn)程url的返回判斷
將以下文本放入一個(gè)HTML頁(yè)面即可看到效果,將會(huì)有兩次彈出提示,最后在頁(yè)面上顯示YES,表示完成2012-01-01

