spring mvc ajax 400解决
生活随笔
收集整理的這篇文章主要介紹了
spring mvc ajax 400解决
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
The request sent by the client was syntactically incorrect.
ajax發起請求時報400錯誤。請求代碼如下:
var reportId=($(obj).parent().parent().parent().children(":first").attr("value"));var isChecked=$(obj).prop("checked")=="checked"?1:0;var reportSetting=$(obj).attr("value");var setting={reportId:reportId,isChecked:isChecked,reportSetting:reportSetting};console.log(JSON.stringify(setting));$.ajax({type: "POST",url: "/reportConfiguration",contentType:"application/json",data:JSON.stringify(setting),dataType: "json",success: function (msg) {if (msg.isSuccess){$("#msg").html("設置成功")}else{$("#msg").html(msg.result);}}});服務端代碼:
@RequestMapping("/reportConfiguration")@ResponseBodypublic String reportSet(@RequestBody ReportSettingEditBean reportSettingEditBean,HttpServletRequest request){return "";}bean定義:
public class ReportSettingEditBean {private long reportId;private boolean isChecked;private ReportSetting reportSetting;public long getReportId() {return reportId;}public void setReportId(long reportId) {this.reportId = reportId;}public boolean isChecked() {return isChecked;}public void setChecked(boolean isChecked) {this.isChecked = isChecked;}public ReportSetting getReportSetting() {return reportSetting;}public void setReportSetting(ReportSetting reportSetting) {this.reportSetting = reportSetting;} }public enum ReportSetting {Fixed(1),Scroll(2),First(4);private int value;public int getValue() {return value;}ReportSetting(int value){this.value=value;} }解決:
在js中核對數據類型與接收數據類中屬性的數據類型是否一致。
上例中:ReportSetting 是枚舉對象, 而var reportSetting=$(obj).attr("value") 是字符串。修改成整數即可。正確請求如下:
var reportId=($(obj).parent().parent().parent().children(":first").attr("value"));var isChecked=$(obj).prop("checked")=="checked"?1:0;var reportSetting=parseInt($(obj).attr("value"));var setting={reportId:reportId,isChecked:isChecked,reportSetting:reportSetting};$.ajax({type: "POST",url: "/reportConfiguration",contentType:"application/json",data:JSON.stringify(setting),dataType: "json",success: function (msg) {if (msg.isSuccess){$("#msg").html("設置成功")}else{$("#msg").html(msg.result);}}});?
總結
以上是生活随笔為你收集整理的spring mvc ajax 400解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女人梦到别人抓蛇是什么意思
- 下一篇: C/C++ 如何劫持别人家的命令||函数