在 Entity Framework 核心 API 上使用 Angular 的 Http Post 请求不起作用
Http Post request using Angular on Entity Framework Core API does not work
我正在尝试从我的 Angular 应用程序发送 Http.Post 请求,以将项目 ("user") 添加到我的数据库。
问题似乎是我的 "add user" 服务存储提供此 http.post 请求的方法对我提供后端的 dotnet 核心 API 没有任何影响。
类似的结构化 Http.Get 请求工作正常,dotnet 核心 API 后端正在从数据库发送数据。
此外,dotnet 核心 API 后端似乎工作正常。从 Postman 发送相同的 http.post 请求(相同的 Body 和相同的地址)会给数据库带来一个新用户。
这是代码,也许你可以帮助我:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { HttpHeaders } from '@angular/common/http';
@Injectable()
export class AddUserService {
constructor(private _http: Http) { }
addUsers() {
console.log('MethodAddUseractive')
return this._http.post('https://localhost:5001/api/Use',
{"id":41,"name":"Dustin Nachname","email":"dn@gmail.com", "genderid": 1,"bday":"1987-09-10T08:15:14.390102","registration_Day":"2019-12-08T00:00:00.000000"})
.map(res => res.json());
}
}
此外,正如我从 Webconsole 条目中看到的那样,方法 adduser() 被调用 "MethodAddUseractive"
您没有包含任何 header。根据您的后端,您必须设置 header 的属性。类似于
var headers = new HttpHeaders({ "Content-Type": "application/json" });
return this._http.post('https://localhost:5001/api/Use',
{"id":41,"name":"Dustin Nachname","email":"dn@gmail.com", "genderid": 1,"bday":"1987-09-10T08:15:14.390102","registration_Day":"2019-12-08T00:00:00.000000"},
{
headers
}
)
.subscribe(res=>console.log(res));
}
我正在尝试从我的 Angular 应用程序发送 Http.Post 请求,以将项目 ("user") 添加到我的数据库。 问题似乎是我的 "add user" 服务存储提供此 http.post 请求的方法对我提供后端的 dotnet 核心 API 没有任何影响。 类似的结构化 Http.Get 请求工作正常,dotnet 核心 API 后端正在从数据库发送数据。 此外,dotnet 核心 API 后端似乎工作正常。从 Postman 发送相同的 http.post 请求(相同的 Body 和相同的地址)会给数据库带来一个新用户。 这是代码,也许你可以帮助我:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { HttpHeaders } from '@angular/common/http';
@Injectable()
export class AddUserService {
constructor(private _http: Http) { }
addUsers() {
console.log('MethodAddUseractive')
return this._http.post('https://localhost:5001/api/Use',
{"id":41,"name":"Dustin Nachname","email":"dn@gmail.com", "genderid": 1,"bday":"1987-09-10T08:15:14.390102","registration_Day":"2019-12-08T00:00:00.000000"})
.map(res => res.json());
}
}
此外,正如我从 Webconsole 条目中看到的那样,方法 adduser() 被调用 "MethodAddUseractive"
您没有包含任何 header。根据您的后端,您必须设置 header 的属性。类似于
var headers = new HttpHeaders({ "Content-Type": "application/json" });
return this._http.post('https://localhost:5001/api/Use',
{"id":41,"name":"Dustin Nachname","email":"dn@gmail.com", "genderid": 1,"bday":"1987-09-10T08:15:14.390102","registration_Day":"2019-12-08T00:00:00.000000"},
{
headers
}
)
.subscribe(res=>console.log(res));
}