Angular 2 http post null Web Api Core
Angular 2 http post null Web Api Core
我正在angular 2中实现一个新的web应用程序,使用现有的web Api (ASP. js)。. NET核心),但我有麻烦与HTTP Post。在搜索了各种信息之后,我仍然无法解决这个问题,我的Web API仍然从angular post接收空参数。
我要看看这里出了什么问题。下面是我的Angular 2代码:
posted(event) {
@Injectable()
export class httpTestComponent {
constructor(public http: Http) {
};
posted(event) {
var user: UserViewModel = {
name: "angularUser",
password: "pw",
email: "angularMail"
}
let pedido = JSON.stringify(user);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post('http://localhost:10832/api/Account/Register', { pedido }, options)
.map(this.extractData).catch(this.handleError).subscribe();
};
ViewModel:
export interface UserViewModel {
name: string,
password: string,
email: string,
}
Web API: [HttpPost]
[Route("Register")]
public void Register([FromBody] UserRegisterViewModel userVm)
{
}
ViewModel WEB API:
public class UserRegisterViewModel
{
public string name { get; set; }
public string password { get; set; }
public string email { get; set; }
}
谁能告诉我我错在哪里?
不需要添加JSON.stringify()
。试着把它去掉
删除JSON.stringify()
或删除体数据周围的大括号:
this.http.post('http://localhost:10832/api/Account/Register', pedido, options)
Angular会尝试将已经序列化的JSON字符串序列化为对象,如果你不这么做,这就是问题所在。
源代码:Link
相关文章:
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 如何在visualstudio中调试web api时编辑javascript文件
- 将复杂对象从angular js传递到web api,它总是返回404
- 将文档上载到Web API
- 对/Token的ASP Web API调用间歇性
- 从其他域和计算机发布的 Web API 帖子
- Web API 调用在设备模式下失败
- Testing .NET Web API from JavaScript
- 将日期作为从 Web API 到 angularJS 的 json 响应
- 获取 Spotify 用户 [Web API] 当前播放的曲目名称
- 在我的 Web API 中无法获取我的自定义标头
- Json 参数强制转换继承 Web API 2.
- 使用来自AngularJS的Web API JSON响应-错误:应为和数组,但得到了一个对象
- Javascript in Visual studio Web Api
- 获取AJAX中未定义的值(x),web api
- Web API在JSON调用上返回XML
- 将JSON字符串发布到WEB API
- 如何通过AngularJS$http从ASP.Net Web API 2获取访问令牌
- 使用ASP.Net MVC Web Api将JSON数据导出到Excel工作表
- 从mvc web api httpresponse生成csv,并通过angularjs接收下载