angular - Angular 7不将发布数据发送到api
问题描述
我有一个asp.net
后端服务。后端 api 工作正常postman
但是从角度做同样的事情是行不通的。正文不会发送到服务器。我正在使用角度 7。
这是我的代码。
乳制品服务
public addNewDairy(value) {
return this.http.post(this.apiUrl + "dairy", value)
.pipe(
map(success => success),
catchError(this.hes.handleError) // then handle the error
);
}
和组件
requestForRegistration(value) {
this.jqxLoader.open();
this.ds.addNewDairy(value).subscribe(res => {
this.jqxLoader.close();
this.message = "Requested for activation.";
this.notification.open();
}, error => {
console.log(error);
this.jqxLoader.close();
this.message = "Something went wrong please try again!";
this.notification.open();
});
}
解决方案
204 来自浏览器作为跨域请求的一部分发送的 OPTIONS 请求。一旦 OPTIONS 请求以 200 成功返回,您的真实请求就会上升。Postman 不会发送 OPTIONS 请求,因为在该实例中您将直接访问 API。您的 Angular 看起来不错 - 我会在您的 asp.net 后端验证是否启用了 CORS - 请参见此处https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-请求-in-web-api#enable-cors。
推荐阅读
- c++ - 如何在另一个类中重载运算符 ==
- python - 使用参考列在数据框中的行值之间进行选择?
- python - 带有 for 循环的 Python 代码不起作用
- keras - Got Very Different Scores After Translating Simple Test Model from Keras to PyTorch
- excel - I need help assigning names to an entire column
- sql-server - 无法获取 Get-SQLAgent 服务器属性
- java - Java中的二项式在获取用户输入后将2个变量保存在一个变量中
- sql-server - PivotTable issue on sqlserver- Data getting split to multiple rows
- angular - 以角度执行开始时下载的文件?
- snakemake - Snakemake: Is it possible to only display "Job counts" using --dryrun?