轉(zhuǎn)自:http://blog.csdn.net/Q_AN1314/article/details/51735184
使用jQuery從前端向后臺(tái)發(fā)送JSON數(shù)據(jù),設(shè)置了Content-Type為application/json,同時(shí)在Spring MVC的Controller里面使用了@RequestParam注解來接收參數(shù),但是只在GET請求的時(shí)候才能正常訪問,在使用POST請求的時(shí)候會(huì)產(chǎn)生找不到參數(shù)的異常。
原因是,jQuery發(fā)送的數(shù)據(jù)是用&符號(hào)連接起來的,像是這種:
這里寫圖片描述
對應(yīng)的Content-Type是 application/x-www-form-urlencoded,但是在ajax中錯(cuò)誤地將Content-Type設(shè)置成了application/json,所以出現(xiàn)了這種錯(cuò)誤。
解決方法有兩種,一種是ajax中發(fā)送數(shù)據(jù)時(shí)加上雙引號(hào),這樣數(shù)據(jù)的格式就是這樣的:
這里寫圖片描述
此時(shí)把Content-Type設(shè)置成application/json就可以了。另一種就是用&連接的數(shù)據(jù),此時(shí)把Content-Type設(shè)置成application/x-www-form-urlencoded。
總之,數(shù)據(jù)的格式一定要和Content-Type保持一致。