angular - Angular 将字符串发布到 asp.net 后端
问题描述
我有一个带有 asp.net 后端的 Angular 项目。我喜欢做的是,简单地将字符串发布到我的控制器。
我尝试了以下方法:
角
constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) { }
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
postString(data:string) {
this.http.post(this.baseUrl + 'api/utility/GetGuid/', "test", this.httpOptions)
.subscribe((guid: string) => {
//do something
});
}
后端:
[Route("api/[controller]")]
public class UtilityController : Controller
{
[HttpPost]
[Route("GetGuid")]
public JsonResult GetGuid([FromBody]string data)
{
//do something
}
这似乎不起作用。不知道如何实现,我只是发布字符串并在后端获取它。
希望您能够帮助我。
解决方案
尝试设置标题Content-Type: application/json; charset=utf-8
推荐阅读
- jpa - 如何使用 CMT 在 Jboss 7 中同时使用 JDBC 和 CMT?
- javascript - 简单的 navigator.clipboard.writeText() 不起作用
- typescript - 将函数参数类型限制为对象中的键
- css - CSS“转义”语法图混乱
- python - axios 根据用户数据获取请求失败
- google-sheets - 如何拆分电子邮件地址的主表
- mysql - 用户 'repl_user'@'10.0.0.5' 的访问被拒绝,但 ip 10.0.0.5 来自哪里?
- rust - Make 函数同时接受 Range 和 RangeInclusive
- excel - 字符串数组中的代码插入的公式未计算
- wordpress - Tor 不接收 CSS 和 JS 等(仅限本地)