ajax方法中的參數:
contentType:發送至服務器時內容的編碼類型,
一般默認:
application/x-www-form-urlencoded
(適應大多數的場合)
dataType:預期服務器返回的數據類型
有時候前台ajax向后台傳數據的時候,能夠進到后台,但是后台顯示接受的參數為
null
,如果ajax沒有問題,這個時候就要考慮后台在接收參數的時候使用的是什么注解。(@RequestParam還是@RequestBody)
這樣就可以解決后台接不到參數的問題啦!!!!
public HttpResponseEntity selectByCollege(@RequestParam Map<String,Object> collegeName) {
使用
@RequestParam
時
:
(推薦這個)
contentType:"
application/x-www-form-urlencoded
"
data:{'college',college}
使用
@RequestParam
,可以不用寫contentType,
application/x-www-form-urlencoded是contentType的默認值
使用
@RequestBody
時:
接收的是json字符串格式的數據,
需要將
contentType寫成:'
application/json
',
data:Json.Stringly(da)(將對象變成字符串)
let da = {'state':state};
-
$.ajax({
-
type:
"POST",
-
url: httpUrl +
"/insertInfo",
-
dataType:
'json',
-
data: JSON.stringify(da),
-
contentType:
"application/json",
-
success:
function (result) {
-
console.log(result);
-
$(
'#ModalInfo').modal(
'hide');
-
-
},
-
error:
function () {
-
console.log(
'錯誤')
-
}
-
})
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
