老师我昨天的问题没有表述请楚
来源:8-1 商品添加之Dao层的实现
慕瓜9365271
2019-08-20
老师您好,我控制层的addproductcategorys方法现在不管是post还是get请求都会爆出400错误,查询相关网页说是ajax的数据问题和后台不匹配,但我这个方法参数是list集合,传过来时同样是list集合.
1.controller的批量增加方法:
@RequestMapping(value = "addproductcategorys" ,method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> addproductcategorys(@RequestBody @RequestParam("list") List<ProductCategory> list, HttpServletRequest request){
Map<String,Object> modelMap =new HashMap<>();
request.getSession().setAttribute("currentShop",1L);
Shop currentShop =(Shop) request.getSession().getAttribute("currentShop");
for (ProductCategory pc:list){
pc.setShopId(currentShop.getShopId());
}
if (list!=null&&list.size()>0){
try{
ProductCategoryExecution pe =productCategoryService.batchAddproductCategory(list);
if (pe.getState()==ProductCategoryStateEnum.SUCCESS.getState()){
modelMap.put("success",list);
}else{
modelMap.put("success",false);
modelMap.put("errMsg",pe.getStateInfo());
}
}catch (ProductCategoryOperationException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
}else {
modelMap.put("success",false);
modelMap.put("errMsg","请至少输入一个店铺");
}
return modelMap;
}
2.我的删除和getList方法都可以用,就是批量增加不能用
批量增加对应的js文件如下:
var addUrl = 'addproductcategorys';
$('#new')
.click(
function() {
var tempHtml = '<div class="row row-product-category temp">'
+ '<div class="col-33"><input class="category-input category" type="text" placeholder="分类名"></div>'
+ '<div class="col-33"><input class="category-input priority" type="number" placeholder="优先级"></div>'
+ '<div class="col-33"><a href="#" class="button delete">删除</a></div>'
+ '</div>';
$('.category-wrap').append(tempHtml);
});
$('#submit').click(function() {
var tempArr = $('.temp');
var list = [];
tempArr.map(function(index, item) {
var tempObj = {};
tempObj.productCategoryName = $(item).find('.category').val();
tempObj.priority = $(item).find('.priority').val();
if (tempObj.productCategoryName && tempObj.priority) {
list.push(tempObj);
}
});
$.ajax({
url : addUrl,
type : 'POST',
data : JSON.stringify(list),
contentType : 'application/json;charset=utf-8',
success : function(data) {
if (data.success) {
$.toast('提交成功!');
getList();
} else {
$.toast('提交失败!');
}
}
});
});
3.这个是我的点提交时报的错误
4.Fiddler的抓包情况
5.排除Url问题,可能是前后台数据不匹配,但是也不知道是怎么回事.希望老师您帮我判断一下,咱们师徒一起把这个问题解决.
写回答
2回答
-
翔仔
2019-08-20
感谢大牛同学的指出,确实是这样的问题,感觉同学是不是用了跨域?
https://blog.csdn.net/bellah521/article/details/71211364
这里面说的可能是这个原因,建议同学直接赋值粘贴我的controller html 和js,事成后,再对比下差别在哪里
controller
package com.imooc.o2o.web.shopadmin; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.imooc.o2o.dto.ProductCategoryExecution; import com.imooc.o2o.dto.Result; import com.imooc.o2o.entity.ProductCategory; import com.imooc.o2o.entity.Shop; import com.imooc.o2o.enums.ProductCategoryStateEnum; import com.imooc.o2o.exceptions.ProductCategoryOperationException; import com.imooc.o2o.service.ProductCategoryService; @Controller @RequestMapping("/shopadmin") public class ProductCategoryManagementController { @Autowired private ProductCategoryService productCategoryService; @RequestMapping(value = "/getproductcategorylist", method = RequestMethod.GET) @ResponseBody private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request) { Shop currentShop = (Shop) request.getSession().getAttribute("currentShop"); List<ProductCategory> list = null; if (currentShop != null && currentShop.getShopId() > 0) { list = productCategoryService.getProductCategoryList(currentShop.getShopId()); return new Result<List<ProductCategory>>(true, list); } else { ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR; return new Result<List<ProductCategory>>(false, ps.getState(), ps.getStateInfo()); } } @RequestMapping(value = "/addproductcategorys", method = RequestMethod.POST) @ResponseBody private Map<String, Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryList, HttpServletRequest request) { Map<String, Object> modelMap = new HashMap<String, Object>(); Shop currentShop = (Shop) request.getSession().getAttribute("currentShop"); for (ProductCategory pc : productCategoryList) { pc.setShopId(currentShop.getShopId()); } if (productCategoryList != null && productCategoryList.size() > 0) { try { ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategoryList); if (pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()) { modelMap.put("success", true); } else { modelMap.put("success", false); modelMap.put("errMsg", pe.getStateInfo()); } } catch (ProductCategoryOperationException e) { modelMap.put("success", false); modelMap.put("errMsg", e.toString()); return modelMap; } } else { modelMap.put("success", false); modelMap.put("errMsg", "请至少输入一个商品类别"); } return modelMap; } @RequestMapping(value = "/removeproductcategory", method = RequestMethod.POST) @ResponseBody private Map<String, Object> removeProductCategory(Long productCategoryId, HttpServletRequest request) { Map<String, Object> modelMap = new HashMap<String, Object>(); if (productCategoryId != null && productCategoryId > 0) { try { Shop currentShop = (Shop) request.getSession().getAttribute("currentShop"); ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId()); if (pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()) { modelMap.put("success", true); } else { modelMap.put("success", false); modelMap.put("errMsg", pe.getStateInfo()); } } catch (ProductCategoryOperationException e) { modelMap.put("success", false); modelMap.put("errMsg", e.toString()); return modelMap; } } else { modelMap.put("success", false); modelMap.put("errMsg", "请至少选择一个商品类别"); } return modelMap; } }
js
$(function() { var listUrl = '/o2o/shopadmin/getproductcategorylist'; var addUrl = '/o2o/shopadmin/addproductcategorys'; var deleteUrl = '/o2o/shopadmin/removeproductcategory'; getList(); function getList() { $ .getJSON( listUrl, function(data) { if (data.success) { var dataList = data.data; $('.category-wrap').html(''); var tempHtml = ''; dataList .map(function(item, index) { tempHtml += '' + '<div class="row row-product-category now">' + '<div class="col-33 product-category-name">' + item.productCategoryName + '</div>' + '<div class="col-33">' + item.priority + '</div>' + '<div class="col-33"><a href="#" class="button delete" data-id="' + item.productCategoryId + '">删除</a></div>' + '</div>'; }); $('.category-wrap').append(tempHtml); } }); } $('#new') .click( function() { var tempHtml = '<div class="row row-product-category temp">' + '<div class="col-33"><input class="category-input category" type="text" placeholder="分类名"></div>' + '<div class="col-33"><input class="category-input priority" type="number" placeholder="优先级"></div>' + '<div class="col-33"><a href="#" class="button delete">删除</a></div>' + '</div>'; $('.category-wrap').append(tempHtml); }); $('#submit').click(function() { var tempArr = $('.temp'); var productCategoryList = []; tempArr.map(function(index, item) { var tempObj = {}; tempObj.productCategoryName = $(item).find('.category').val(); tempObj.priority = $(item).find('.priority').val(); if (tempObj.productCategoryName && tempObj.priority) { productCategoryList.push(tempObj); } }); $.ajax({ url : addUrl, type : 'POST', data : JSON.stringify(productCategoryList), contentType : 'application/json', success : function(data) { if (data.success) { $.toast('提交成功!'); getList(); } else { $.toast('提交失败!'); } } }); }); $('.category-wrap').on('click', '.row-product-category.temp .delete', function(e) { console.log($(this).parent().parent()); $(this).parent().parent().remove(); }); $('.category-wrap').on('click', '.row-product-category.now .delete', function(e) { var target = e.currentTarget; $.confirm('确定么?', function() { $.ajax({ url : deleteUrl, type : 'POST', data : { productCategoryId : target.dataset.id }, dataType : 'json', success : function(data) { if (data.success) { $.toast('删除成功!'); getList(); } else { $.toast('删除失败!'); } } }); }); }); });
html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>商品分类管理</title> <meta name="viewport" content="initial-scale=1, maximum-scale=1"> <link rel="shortcut icon" href="/favicon.ico"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css"> <link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css"> <link rel="stylesheet" href="../resources/css/shop/productcategorymanagement.css"> </head> <body> <header class="bar bar-nav"> <a class="button button-link button-nav pull-left back" href="javascript:history.back(-1)"> <span class="icon icon-left"></span> 返回 </a> <h1 class="title">商品分类管理</h1> </header> <div class="content"> <div class="content-block"> <div class="row row-product-category"> <div class="col-33">类别</div> <div class="col-33">优先级</div> <div class="col-33">操作</div> </div> <div class="category-wrap"></div> </div> <div class="content-block"> <div class="row"> <div class="col-50"> <a href="#" class="button button-big button-fill button-success" id="new">新增</a> </div> <div class="col-50"> <a href="#" class="button button-big button-fill" id="submit">提交</a> </div> </div> </div> </div> <script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script> <script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script> <script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script> <script type='text/javascript' src='../resources/js/shop/productcategorymanagement.js' charset='utf-8'></script> </body> </html>
00 -
突出部作战
2019-08-20
哥们感觉你写得有点问题,因为你看截图里面你传递给后台是走的GET而不是POST,应该是POST才对,可以用我的替换下你的看看
$('#submit').click(function() { var tempArr = $('.temp'); var productCategoryList = []; tempArr.map(function(index, item) { var tempObj = {}; tempObj.productCategoryName = $(item).find('.category').val(); tempObj.priority = $(item).find('.priority').val(); if (tempObj.productCategoryName && tempObj.priority) { productCategoryList.push(tempObj); } }); $.ajax({ url : addUrl, type : 'POST', data : JSON.stringify(productCategoryList), contentType : 'application/json', success : function(data) { if (data.success) { $.toast('提交成功!'); getList(); } else { $.toast('提交失败!'); } } }); });
012019-08-20
相似问题