SpringMVC接收Ajax請求幾種常用方式

2021-10-07 21:05:37 字數 3042 閱讀 7563

幾種常用的springmvc接收ajax請求方式,

引數型別以及contenttype型別的區分

1.@requestparam

2.@requestbody

3.引數與前端請求匹配

4.物件接收

get請求都可以使用以下兩種方式進行請求:

$.

ajax(,

url:

'/test/testrequestparamget'

, datatype:

'json'

, success:

function

(data)

})

$.

ajax(}

)

1.requestparam方式接收

(value =

"/testrequestparamget"

, method = requestmethod.get)

@responsebody

public ajaxres testrequestparamget

(@requestparam

(value =

"userid"

) string userid)

throws exception

2.引數匹配

(value =

"/testautoget"

, method = requestmethod.get)

@responsebody

public ajaxres testautoget

(string userid)

throws exception

3.物件實體

(value =

"/testbodyget"

, method = requestmethod.get)

@responsebody

public ajaxres testbodyget

(user user)

throws exception

前端請求

$.

ajax(}

)

$.

ajax(,

url:

'/test/testrequestparampost'

, contenttype:

, datatype:

'json'

, success:

function

(data)

})

後端接收

(value =

"/testrequestparampost"

, method = requestmethod.post)

@responsebody

public ajaxres testrequestparampost

(@requestparam

(value =

"userid"

) string userid)

throws exception

前端請求

$.

ajax()

, url:

'/test/testrequestbodypost'

, contenttype:

, datatype:

'json'

, success:

function

(data)

})

後端接收

(value =

"/testrequestbodypost"

, method = requestmethod.post)

@responsebody

public ajaxres testrequestbodypost

(@requestbody user user)

throws exception

前端請求

$.

ajax(,

url:

'/test/testautopost'

, contenttype:

, datatype:

'json'

, success:

function

(data)

})

$.

ajax(}

)

後端接收

(value =

"/testautopost"

, method = requestmethod.post)

@responsebody

public ajaxres testautopost

(string userid)

throws exception

前端請求

$.

ajax(,

url:

'/test/testbodypost'

, contenttype:

, datatype:

'json'

, success:

function

(data)

})

後端接收

(value =

"/testbodypost"

, method = requestmethod.post)

@responsebody

public ajaxres testbodypost

(user user)

throws exception

1.get請求:

前端可以使用url拼接引數形式或者表單形式提交

後端可以使用@requestparam,引數匹配接收,物件接收

get請求不可以使用@requestbody接收

2.post請求

前端採用url拼接引數形式提交,後端使用@requestparam方式,引數匹配方式接收

springmvc接收ajax請求引數遇到的坑

做專案的過程中,springmvc controller接收前端ajax請求引數總是接收不到,為null值,具體 如下 前端ajax ajax url getcontextpath groupuserrel insert data datatype json type post success fu...

spring mvc 接收ajax傳遞來的陣列物件

前幾天做批量刪除的時候,獲取的是id陣列,所以在後台control裡接收時一直接收不到,將他轉換為字串就可以了 json.stringify data js function deleted dgid,url,key else ajax datatype json success function ...

Spring MVC接收引數

1.和servletapi緊耦方法 使用原生 servletapi 使用原有的request物件進行獲取 只需要在方法內容宣告request形參即可,servlet在呼叫的時候會自動賦值。按照request的使用方式正常使用即可 springmvc引數優點 沒有引數,不賦值,正常執行 有引數,則給引...