【发布时间】:2019-10-22 08:29:11
【问题描述】:
前端使用 Angular 6,后端使用 Aps Web Api
我在 ASP 中准备了简单的 CRUD 操作,在 Postman 中可以正常工作。 |*> 在 Postman 中使用
=> Select "Body" Tab
=> Select "raw" Radio Button
=> Select "JSON (application/json)" from Dropdown
但在尝试使用角度发布时出现以下错误
zone.js:2969 OPTIONS "http://localhost:8008/api/user" 405(不允许的方法)
localhost/:1 从源 'http://localhost:4200' 访问 XMLHttpRequest 在 '"http://localhost:8008/api/user"' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。
我试过用 Chrome 浏览器启用 CORS 插件
尝试 1:
const headers = new HttpHeaders().set('content-type', 'application/json');
let userData =
{
"name":"Suji",
"email":"suji@yahoo.com",
"mobile":"9864224"
}
this.httpClient.post("http://localhost:8008/api/user",userData,{headers})
尝试 2:
const httpOptions = { headers: new HttpHeaders({
'Content-Type': 'application/json'
})};
let userData =
{
"name":"Suji",
"email":"suji@yahoo.com",
"mobile":"9864224"
}
this.httpClient.post("http://localhost:8008/api/user",userData,httpOptions);
尝试 3:尝试使用 stringyfy
const headers = new HttpHeaders().set('content-type', 'application/json');
let userData =
{
"name":"Suji",
"email":"suji@yahoo.com",
"mobile":"9864224"
}
this.httpClient.post("http://localhost:8008/api/user",JSON.stringify(userData),{headers})
尝试 4:尝试使用 stringyfy
const httpOptions = { headers: new HttpHeaders({
'Content-Type': 'application/json'
})};
let userData =
{
"name":"Suji",
"email":"suji@yahoo.com",
"mobile":"9864224"
}
this.httpClient.post("http://localhost:8008/api/user",JSON.stringify(userData),httpOptions);
让我知道我在哪里丢失或需要设置什么
【问题讨论】:
标签: angular asp.net-web-api cors http-headers