話說angular的傳參轉(zhuǎn)變?yōu)閖son格式,而又需要springMVC有相關(guān)的實(shí)體類別才能用@requestBody去接收。但是如果傳的是一般的參數(shù)呢,例如一個(gè)數(shù)組,一個(gè)數(shù)字id?
當(dāng)然,傳一個(gè)數(shù)字id可以從請(qǐng)求參數(shù)路徑傳過去。但是,就是傳一般的隨機(jī)的參數(shù),例如data={“xx”,xxx}
和明顯,springMVC端是不能像接收jquery時(shí)候用@requestParam接收的。
網(wǎng)路上看了一大片,說要改請(qǐng)求的header的contentType什麼的,可是,設(shè)定了也沒用.......怎麼辦?
$http({
url:"ts",
data={“xx”,xxx},
type:"POST"
})
........
spirngMVC端如何接收data={“xx”,xxx},
前臺(tái)代碼:
angular.module('app')
.factory('Email', function ($resource) {
return $resource('api/emails/:id', {}, {
'query': { method: 'GET', isArray: true},
'get': {method: 'GET'},
'update': { method:'PUT' }
});
});
後臺(tái)接收
@RequestMapping(value = "/emails",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<EmailDTO> createEmail(@RequestBody EmailDTO emailDTO) {.....}
問題不是angular怎麼傳參數(shù)到後臺(tái),主要是看你後臺(tái)怎麼實(shí)現(xiàn)的。後臺(tái)給一個(gè)介面對(duì)於angular就夠了,angular直接$http調(diào)後臺(tái)介面就能把參數(shù)傳給後臺(tái)了。
$http.get("mainUrl"+parameter).then();
這樣就可以把參數(shù)parameter傳給後臺(tái)了,至於是get還是post或者其他的主要還是看後臺(tái)。