ajax提交异步验证
2014-03-19 10:12
417 查看
简单实用的ajax异步校验代码
jqueyr代码:
function submitPriceInfo(){
var price = $("#price").val();
var amount = $("#amount").val();
var pbillId = $("#pbillId").val();
var curentId = "$!enterprise.id";
var billId = "$!tTraPbill.enterpirse.id";
if(curentId==billId){
alert("不能给自己报价!!");
}else{
$.ajax({
type : "post",
url : "$!webPath/usercenter/delegateManage/submitPprice.htm",
data : {price:price,amount:amount,pbillId:pbillId},
async : false,
success : function(data){
var codes = jQuery.parseJSON(data);
if(codes.code=="0"){
alert("提交不成功!");
}else{
if(codes.code=="1"){
alert("报价提交成功!");
if(confirm("点击确定刷新后将显示委托采购页面!")){
document.location.href="$!webPath/itTraPbillInfo/list.htm";
}
}else{
if(codes.code="2"){
document.location.href="$!webPath/login.htm";
}
}
}
}
});
}
}
注:$!webPath为事先定义好的,为http://localhost:8080/项目名
控制层:
@RequestMapping("/submitPprice.htm")
@ResponseBody
public ResponseEntity<String> submitPprice(HttpServletRequest request,
HttpServletResponse response) {
String json = "";
String price = request.getParameter("price");
String amount = request.getParameter("amount");
String pbillId = request.getParameter("pbillId");
TTraEnterprise enterprise = SecurityUserHolder.getCurrentTTraEnterprise();
if(enterprise==null){
json = "{\"code\":\"2\"}";
}else{
String enterpriseId = enterprise.getId();
TTraUser user = SecurityUserHolder.getCurrentUser();
String userId = user.getId();
TTraPprice traPprice = new TTraPprice();
traPprice.setEnterpriseId(enterpriseId);
traPprice.setPrice(Double.parseDouble(price));
traPprice.setAmount(Double.parseDouble(amount));
traPprice.setPbillId(pbillId);
traPprice.setAddUserId(userId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(new Date());
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
traPprice.setAddTime(date);
traPprice.setStateFlag("0");
TTraPbill ttraPbill = itTraPbillService.getObjById(pbillId);
if(itTraPpriceService.save(traPprice)){
json = "{\"code\":\"1\"}";
if(ttraPbill.getStateFlag().equals("0")){
ttraPbill.setStateFlag("2");
}
if(ttraPbill.getApriceNum()==null || ttraPbill.getApriceNum()==0){
ttraPbill.setApriceNum(1l);
}else{
ttraPbill.setApriceNum(ttraPbill.getApriceNum()+1);
}
itTraPbillService.update(ttraPbill);
}else{
json = "{\"code\":\"0\"}";
}
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/text;charset=UTF-8");
return new ResponseEntity<String>(json, headers, HttpStatus.ACCEPTED);
}
注:关键代码红色标注
jqueyr代码:
function submitPriceInfo(){
var price = $("#price").val();
var amount = $("#amount").val();
var pbillId = $("#pbillId").val();
var curentId = "$!enterprise.id";
var billId = "$!tTraPbill.enterpirse.id";
if(curentId==billId){
alert("不能给自己报价!!");
}else{
$.ajax({
type : "post",
url : "$!webPath/usercenter/delegateManage/submitPprice.htm",
data : {price:price,amount:amount,pbillId:pbillId},
async : false,
success : function(data){
var codes = jQuery.parseJSON(data);
if(codes.code=="0"){
alert("提交不成功!");
}else{
if(codes.code=="1"){
alert("报价提交成功!");
if(confirm("点击确定刷新后将显示委托采购页面!")){
document.location.href="$!webPath/itTraPbillInfo/list.htm";
}
}else{
if(codes.code="2"){
document.location.href="$!webPath/login.htm";
}
}
}
}
});
}
}
注:$!webPath为事先定义好的,为http://localhost:8080/项目名
控制层:
@RequestMapping("/submitPprice.htm")
@ResponseBody
public ResponseEntity<String> submitPprice(HttpServletRequest request,
HttpServletResponse response) {
String json = "";
String price = request.getParameter("price");
String amount = request.getParameter("amount");
String pbillId = request.getParameter("pbillId");
TTraEnterprise enterprise = SecurityUserHolder.getCurrentTTraEnterprise();
if(enterprise==null){
json = "{\"code\":\"2\"}";
}else{
String enterpriseId = enterprise.getId();
TTraUser user = SecurityUserHolder.getCurrentUser();
String userId = user.getId();
TTraPprice traPprice = new TTraPprice();
traPprice.setEnterpriseId(enterpriseId);
traPprice.setPrice(Double.parseDouble(price));
traPprice.setAmount(Double.parseDouble(amount));
traPprice.setPbillId(pbillId);
traPprice.setAddUserId(userId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(new Date());
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
traPprice.setAddTime(date);
traPprice.setStateFlag("0");
TTraPbill ttraPbill = itTraPbillService.getObjById(pbillId);
if(itTraPpriceService.save(traPprice)){
json = "{\"code\":\"1\"}";
if(ttraPbill.getStateFlag().equals("0")){
ttraPbill.setStateFlag("2");
}
if(ttraPbill.getApriceNum()==null || ttraPbill.getApriceNum()==0){
ttraPbill.setApriceNum(1l);
}else{
ttraPbill.setApriceNum(ttraPbill.getApriceNum()+1);
}
itTraPbillService.update(ttraPbill);
}else{
json = "{\"code\":\"0\"}";
}
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/text;charset=UTF-8");
return new ResponseEntity<String>(json, headers, HttpStatus.ACCEPTED);
}
注:关键代码红色标注
相关文章推荐
- js登录特效+ajax提交表单+异步刷新验证
- ajax异步提交以及nicevalidator自定义验证示例
- jquery---------ajax+validate表单异步提交验证
- form表单submit提交时,用ajax做异步验证
- 基于Ajax,异步表单验证,实现有一条件不满足不能提交
- 做注册的验证,在服务器端处理,客服端ajax异步提交数据
- jquery---------ajax+validate表单异步提交验证
- jquery---------ajax+validate表单异步提交验证
- 做注册的验证,在服务器端处理,客服端ajax异步提交数据
- SSM整合之后,用ajax异步提交验证用户名。
- jquery---ajax异步提交+validate表单验证
- 页面验证1——ajax异步提交
- ajax提交异步验证
- Ajax表单异步提交及验证
- JQUERY的表单异步提交(ajax)
- ajax异步提交
- 提交表单时,进行ajax验证
- 【java项目实践】具体解释Ajax工作原理以及实现异步验证username是否存在+源代码下载(java版)
- ajax文件异步提交的实现
- js 异步表单提交,图片上传,兼容异步模拟ajax技术