我使用的是angularjs,我試圖對我的web api進行HttpPost調用。web api 2發佈參數 - 必須使用json.stringyfi
我的API方法:
[HttpPost]
[Route("authentication/getkey")]
public IHttpActionResult GetKey([FromBody]string password) {
//Do stuff
}
我的電話:
service.getKey = function (password) {
return $http.post('api/authentication/getkey', JSON.stringify(password))
.then(function(result) {
return result.data;
});
}
現在這工作得很好,但我真的需要使用JSON.stringify
?我試着像下面這樣發送它,但是它們都是password = null。我必須使用JSON.stringify
還是我在其他示例中做錯了?
//Doesnt work
service.getKey = function (password) {
return $http.post('api/authentication/getkey', password)
.then(function(result) {
return result.data;
});
}
//Doesnt work
service.getKey = function (password) {
return $http.post('api/authentication/getkey', {password})
.then(function(result) {
return result.data;
});
}
您需要將POST請求中的數據作爲JSON格式發送,因爲默認情況下角$ http服務將Content-Type作爲application/json發送。 JSON.stringify會將您的數據轉換爲JavaScript對象表示法(JSON)字符串。有沒有你不想使用JSON.stringify的原因?一種方法是更改內容類型。如果你願意,我可以提供一些示例代碼。 –
@VivekN請做那個。我只想看看如何去做的其他例子 - 或者我該怎麼做 – MrProgram