主頁 > 知識庫 > json格式的Ajax提交示例代碼

json格式的Ajax提交示例代碼

熱門標簽:阿里云 服務器配置 Mysql連接數(shù)設置 銀行業(yè)務 科大訊飛語音識別系統(tǒng) Linux服務器 電子圍欄 團購網(wǎng)站

需要加入: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("掃描二維碼提交結果: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 = "二維碼內(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)境下實現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)
  • django通過ajax發(fā)起請求返回JSON格式數(shù)據(jù)的方法
  • jQuery中使用Ajax獲取JSON格式數(shù)據(jù)示例代碼
  • 如何將ajax請求返回的Json格式數(shù)據(jù)循環(huán)輸出成table形式
  • jsp中利用jquery+ajax在前后臺之間傳遞json格式參數(shù)
  • Jquery Ajax 學習實例2 向頁面發(fā)出請求 返回JSon格式數(shù)據(jù)
  • Ajax與用戶交互的JSON數(shù)據(jù)存儲格式

標簽:廣元 蚌埠 萍鄉(xiāng) 大理 衢州 衡水 江蘇 棗莊

巨人網(wǎng)絡通訊聲明:本文標題《json格式的Ajax提交示例代碼》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266