1. 程式人生 > >POST 400 Bad Request The request sent by the client was syntactically incorrect

POST 400 Bad Request The request sent by the client was syntactically incorrect

最近在做Web開發的時候,使用$.post提交資料,但是回撥函式卻沒有被觸發,按F12看控制檯輸出是:POST *** 400 Bad Request

後臺是SpringMVC的,設定了斷點也不會被觸發。

後來檢視JQuery資料瞭解到,$.post提交資料只有成功時才觸發回撥函式,於是改用$.ajax提交資料,新增error回撥函式,得到錯誤資訊了,如下圖:


這個問題是什麼原因造成的呢?

後來經過測試發現,是表單提交的內容資料型別與實體的(也就是資料表字段)的資料型別不匹配導致的。

在提交表單之前應該對使用者輸入的內容做驗證,後臺直接做映射了,沒有做內容驗證的機會。

2016-05-26更新

解決方法:

上面只是描述了問題產生的原因,而並沒有給出解決的方法,很多小夥伴其實看到原因就已經想到解決方法了。

那有些小夥伴可能剛接觸,還搞不清狀況,我就再說一下解決的步驟:

1、改用$.ajax提交資料,新增error回撥函式(其實在開發中嚴格來說是不允許使用$.post的,因為失去了錯誤處理的功能);

類似程式碼如下,具體請參考JQurey參考手冊:

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   },
    error: function(XMLHttpRequest){
     alert( "Error: " + XMLHttpRequest.responseText);
   }
});
這樣你就可以通過 XMLHttpRequest.responseText 或XMLHttpRequest.responseHTML 瞭解到後端程式碼到底發生了什麼錯誤。

2、不要把生成實體的工作交給MVC框架來完成

你的產生問題的程式碼,我猜測可能類似如下:

	/** 
	 * 新增使用者.
	 **/
	@RequestMapping("/adduser.do")
	@ResponseBody
	public void adduser(HttpServletRequest request, HttpServletResponse response, User user) throws Exception {
		this.userManager.saveOrUpdate(user);
	}
這樣做就導致上面說過的問題:後臺直接做映射了,沒有做內容驗證的機會;明明一個欄位要求數字,結果使用者輸入了字母或漢字。

正確的方法類似如下:

	/** 
	 * 新增使用者.
	 **/
	@RequestMapping("/adduser.do")
	@ResponseBody
	public void adduser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//將形參 user 拿到函式內部定義建立
		User user = new User();
		//這裡對使用者的年齡就要進行判斷,具體規則你自己定義,我只是舉個例子
		Integer age = getInteger("age");
		if (age != null){
			uset.SetAge(age);
		}
		this.userManager.saveOrUpdate(user);
	}
	
	protected Integer getInteger(String name) {
		String str = this.request.getParameter(name);
		if (StringUtils.isNotBlank(str)) {
		  return Integer.valueOf(str);
		}
		return null;
	  }

你的問題解決了嗎?

沒有?

看下面這個案例:

相關推薦

POST 400 Bad Request The request sent by the client was syntactically incorrect

最近在做Web開發的時候,使用$.post提交資料,但是回撥函式卻沒有被觸發,按F12看控制檯輸出是:POST *** 400 Bad Request後臺是SpringMVC的,設定了斷點也不會被觸發。後來檢視JQuery資料瞭解到,$.post提交資料只有成功時才觸發回撥函

錯誤400-The request sent by the client was syntactically incorrect

null row mvc 出了 ash get service fault ogre 出錯部分是學習SpringMVC 時學習https://my.oschina.net/gaussik/blog/640164,在添加博客文章部分出現了這個問題。 The request

ssm 錯誤400-The request sent by the client was syntactically incorrect

錯誤400-The request sent by the client was syntactically incorrect  springMVC中,某個頁面提交時報400錯誤,如下圖: 解決方法: 1.在網上找了一下,答案是通常遇到這個錯誤是因為前端jsp頁面的控制元件名稱和

SpringMVC用POST方式提交資料(包括含時間)時遇到The request sent by the client was syntactically incorrect.

        對於一個剛入手SSM的小白來說學習可謂是步履維艱,這不在修改表單資料並提交時就遇到了The request sent by the client was syntactically incorrect.問題。在網上看了好多回答,有說jar包

spring mvc 資料繫結問題 提交表單提示HTTP status 400, The request sent by the client was syntactically incorrect

我們在spring mvc 中controller方法中的引數,spring mvc會自動為我們進行資料繫結。 spring mvc 方法中不一定要全部都有 form表單提交的屬性, 也可以有 請求屬性中 沒有的引數(這時候只會把對應不上的引數設為null),

The request sent by the client was syntactically incorrect.(轉載)

gif div button btn brief submit label rect method 這個錯誤是SpringMVC報出來的,見到它意味著html/jsp頁面的控件名稱 和 controller裏函數的參數不符。 好比界面有個這樣的form &l

springmvc 上傳檔案的時候.The request sent by the client was syntactically incorrect

 出現這個問題的原因,下面這篇文章已經講得很清楚的。 http://blog.csdn.net/kunkun378263/article/details/41863101 我遇到的場景是:MultipartFile上傳檔案,提交表單的時候除了上傳檔案還有幾個數字。我們知

Spring MVC——The request sent by the client was syntactically incorrect ()的原因與解決辦法

這句話的意思是客戶端傳送的請求語法錯誤,從而導致springmvc無法實現資料繫結。  而這種錯誤呢,大部分是由於引數格式不正確。 下面舉一個例子來說明: <form:form id="seller_changepassword_form" modelAttribute=

SpringMVC:Controller請求報錯The request sent by the client was syntactically incorrect

因為Spring預設的parser是StringParser,而我傳入的是組合類,SpringMVC解析出錯。 @Data public class QQGameGetCouponDto { private BaseParams baseParams; private QQGam

description The request sent by the client was syntactically incorrect.

猜想 png desc cli was http client req string shi用url傳遞參數,其他頁面都ojbk的 唯獨有一個請求 不應該啊 這明顯的已經配置好了啊 因為別的請求我也是這樣配置的啊,運行是沒問題的啊 運行好好的啊 一時間,感覺無從下

The request sent by the client was syntactically incorrect解決

在請求頁面時,瀏覽器報錯:  The request sent by the client was syntactically incorrect的字面意思是:客戶端傳送的請求在語法上是錯誤的。 這個提示不夠詳細,看了一下後臺,後臺給的部分提示如下: Failed

檔案上傳時顯示The request sent by the client was syntactically incorrect ()

前端頁面涉及到檔案上傳的時候,標籤中不只需要action屬性,還需要新增enctype="multipart/form-data"以及規定提交方式method=“post”,即: <form name="documentInsert"enctype="multipart/form-

post 400 (Bad Request)異常怎麼排查引數問題

問題描述: 用ajax請求時報post 400 (Bad Request)的異常,前臺js引數JSON.stringify(data),後臺controller 中@RequestBody XX xx(javabean)接收引數。 通常發生400時,即使在後臺方法上設定斷

[Angular] Read Custom HTTP Headers Sent by the Server in Angular

conf nor update names fault pan table tom color By default the response body doesn’t contain all the data that might be needed in y

【python】【requests】呼叫requests庫post時遇到Post call throwing HTTP 400 Bad Request

python在呼叫requests的post時,http server返回400 Bad Request error; 在post時,使用了resp=requests.post(url=URL,data=payload,headers=headers) 此時,tomcat返回結果為

php curl post請求返回400 bad request

php post請求返回400 bad request,程式碼如下: /** * 模擬post進行url請求 * @param string $url * @param array $post

pojo引數繫結-post請求亂碼/獲取null的請求引數/400 Bad Request.

1.POST請求亂碼 解決方法:在web.xml檔案中新增post編碼過濾器     <filter>         <filter-name>CharacterEncodi

Http的Get/Post請求帶有特殊字元,400 Bad Request解決方案

今天做專案的時候,需要向伺服器介面 傳遞 json引數,使用 apache 的 httpclient,當時沒有對引數做特殊處理直接提交了,結果執行時報錯了,如下: HTTP 400 Bad Request 後來在網上查資料找到原因了:HttpGet或 HttpPost都不能

記解決一次“HTTP Error 400. The request URL is invalid”的錯誤

cat 2.0 get run 情況 -i tle 錯誤信息 重啟 今天將圖片服務切到使用了cdn的機器上面去,然後就部分圖片報如下圖錯誤“HTTP Error 400. The request URL is invalid” 看到這種錯誤信息,一般的開發者心中可能會猜測

urllib2.HTTPError: HTTP Error 400: Bad Request

htm cat flow sca gzip question ica 3.0 top 1 import urllib2 2 import re 3 import os 4 5 def process_item(self, item, spider): 6