【发布时间】:2020-12-03 07:45:50
【问题描述】:
大家!
我想从我的前端发送字符串,我使用 Angular9。代码如下:
const word: string = this.str;
console.log(word);
this.http.post(this.myurl, word)
.subscribe(data => {
console.log(data);
// console.log(this.value)
});
变量word包含字符串(word),通过post请求发送到服务器。 this.myurl 是 URL 字符串,由服务器 URL 组成。 我的后端应该接收并计算这个(单词)字符串。之后,它应该将 json 字符串返回到前面。代码如下:
[HttpPost]
public async Task<ActionResult<string>> Adding([FromBody] string word)
{
string json = string.Empty;
//some calculations with **word** after which I will get dictionary.
json = JsonConvert.SerializeObject(dictionary);
//some calculations
return json;
}
所以,问题是,Angular 在控制台中报告 415 错误。我知道如何在不订阅的情况下发出 post 请求,而我不明白如何在 post 请求中接收 Angular 中的 json。每次我从服务器获得不同的 json 字符串(它实际上取决于你从前面发送的字符串),所以,我不能为此目的构造静态模型。 无论如何,如果你能帮助我解决这个问题,我将非常感激。
【问题讨论】:
-
只要返回对象,框架会为你序列化。这才是重点。我相信从 body 正在寻找一个对象中称为 word 的属性。
标签: c# .net angular asp.net-web-api .net-core