The request sent by the client was syntactically incorrect in spring mvc, ajax
下面是对我的控制器的 ajax 调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $('#submitButton').on('click', function() { var expenseDetailsListVO = {}; expenseDetailsListVO = getData(); console.log(expenseDetailsListVO); var data = JSON.stringify(expenseDetailsListVO); console.log(data); $.ajax({ url :"${pageContext.request.contextPath}/saveExpenses", data : data, method : 'POST', contentType :"application/json", success : function(data) { }, error : function(data) { } }); }); |
这是我的控制器
1 2 3 4 5 6 7 8 9 10 11 12 | @RequestMapping (value ="/saveExpenses", method=RequestMethod.POST) public String saveExpenses (@RequestBody(required=false) ExpenseDetailsListVO expenseDetailsListVO, Model model) { //System.out.println("User Id is........"+userId); System.out.println(expenseDetailsListVO); ExpenseDetailsList expenseDetailsList = MapperUtil.convertExpenseListVOToDO(expenseDetailsListVO); //expenseServiceManagement.addExpense(expenseDetailsList); return"addExpense"; } |
下面是我的java类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | public class ExpenseDetailsListVO { private List<ExpenseDetailsVO> expenseDetailsVOs = LazyList.decorate( new ArrayList<ExpenseDetailsVO>(), FactoryUtils.instantiateFactory(ExpenseDetailsVO.class)); public ExpenseDetailsListVO (){} public List<ExpenseDetailsVO> getExpenseDetailsVOs() { return expenseDetailsVOs; } public void setExpenseDetailsVOs(List<ExpenseDetailsVO> expenseDetailsVOs) { this.expenseDetailsVOs = expenseDetailsVOs; } @Override public String toString() { return"ExpenseDetailsListVO [expenseDetailsVOs=" + expenseDetailsVOs +"]"; } } public class ExpenseDetailsVO { /*private int expenseId;*/ private String itemDescription; private double amount; private Date expenseDate; public ExpenseDetailsVO (){} /*public int getExpenseId() { return expenseId; } public void setExpenseId(int expenseId) { this.expenseId = expenseId; }*/ public String getItemDescription() { return itemDescription; } public void setItemDescription(String itemDescription) { this.itemDescription = itemDescription; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public Date getExpenseDate() { return expenseDate; } public void setExpenseDate(Date expenseDate) { this.expenseDate = expenseDate; } @Override public String toString() { return"ExpenseDetailsVO [itemDescription=" + itemDescription +", amount=" + amount +", expenseDate=" + expenseDate +"]"; } } |
以前它可以工作,但我改变了一些我不记得的东西,现在在浏览器控制台中出现错误
客户端发送的请求语法错误
请各位大佬告诉我是什么问题...
有一个非常小的错误,因此它显示了该错误...
所以我只需要做些小改动,下面是 jquery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $('#submitButton').on('click', function() { var expenseDetailsListVO = {}; expenseDetailsListVO = getData(); console.log(expenseDetailsListVO); var data = JSON.stringify(expenseDetailsListVO); console.log(data); $.ajax({ url :"${pageContext.request.contextPath}/saveExpenses", data : data, method : 'POST', contentType :"application/json", success : function(data) { }, error : function(data) { } }); }); |
我的控制器期待一个对象,该对象将具有对象数组,但从客户端发送数组,因此该数组需要package在我所做的对象及其工作中:)