【发布时间】:2020-09-29 06:43:34
【问题描述】:
现在我正在尝试提出一个简单的发布请求。这是表格
<label for="name">Name</label>
<input type="text" id="value" [(ngModel)]="value" />
<input type="button" value="Get Name" (click)="getName(value)" />
这里是 .ts
import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-home',
templateUrl: './add-name.component.html'
})
export class FetchNameComponent {
chosenName: string;
constructor(
private http: HttpClient
)
{
}
getName(value): void {
this.chosenName = value
alert(this.chosenName)
this.http.post("NameItem", this.chosenName).subscribe(result => {
console.log(result);
}, error => console.error(error));
}
}
我的警报向我显示了正确的值,所以我知道到目前为止一切都很好
这是我的控制器
[HttpPost]
public String Post(String chosenName)
{
string newName = chosenName + "123";
return newName;
}
chosenName 始终为 null,因此有问题。我很好奇如何使用 angular2 将类型和对象发送到 post 请求中
如果我想通过它发送一个对象,它看起来是否相似?
【问题讨论】:
标签: angular typescript post model-view-controller angular2-forms