JSON 형식의 Ajax 제출 샘플 코드

韦小宝
풀어 주다: 2017-12-31 10:18:48
원래의
1393명이 탐색했습니다.

이 글에서는 주로 json 형식의 Ajax 제출을 소개합니다. 일부 jar 패키지와 js 패키지를 소개해야 합니다. js 샘플 코드는 다음과 같습니다. js에 관심 있는 친구들은 함께 살펴보세요!

가입 필요: 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"> 
//扫描二维码 
$(&#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(); 
} 
}
로그인 후 복사


이것이 전부입니다. 기사 모든 코드가 모든 사람에게 도움이 되기를 바랍니다! !

관련 권장 사항:

jquery 데이터를 여러 번 요청할 때 ajax가 새로 고쳐지지 않는 문제를 해결하는 방법

ajax 새로 고침 없음 확인 등록 정보 예

ajax에서 반환된 세 가지 데이터 유형을 처리하는 방법 서버


위 내용은 JSON 형식의 Ajax 제출 샘플 코드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!