飞行前响应没有HTTP正常状态。 403在Angular 6中

时间:2018-10-10 18:41:29

标签: angular rest cors angular6 preflight

我正在尝试发出PUT请求,但出现以下错误:

Failed to load <localhost:8080/uaa/groups/dfsfaes8323df32>: Response for preflight does not have HTTP ok status.

显然,当我发出GET和POST请求并在邮递员中工作时,它可以工作,但不适用于我代码中的PUT请求。

这是我的要求:

let link = "https://ice.<mydomain>.com/uaadev/Groups/{groupId}";
link = link.replace("{groupId}", data);
const updLoad = {
  displayName: this.groupInfoObjects.displayName,
  description: this.groupInfoObjects.description,
  zoneId : "uaa",
  schemas: [
 "urn:scim:schemas:core:1.0"
  ]
};
let Header = new Headers({
  Authorization: `Bearer {token}`.replace("{token}", this.token),
  "Content-Type": "application/json",
  Accept: "application/json",
  "if-Match":"*"
});
let Option = new RequestOptions({ headers: Header });

this.http.put(link, updLoad, Option).subscribe(
  res => {
    console.log(res);
    console.log(res.status);
    if (res.status === 200) {
 this.fetchGroupInfo(this.dataservice.getDTO("GroupId"));
      swal(" Updated Successfully");
    }
  },
  error => {
    console.log("errroroorororororor");
    console.log("error object " +error);
  }
);

2 个答案:

答案 0 :(得分:3)

the process for solving these process is listed below:

1.create a proxy.conf.json file 
2.set it up like this:copy and paste the below code
{
  "/dev/*": {
    "target": "https://google.com/",
    "secure": false
  },
  "changeOrigin": false,
  "logLevel": "debug"
}

3. add this to your package.json :
"start": "ng serve --proxy-config ../Activated/proxy.conf.json",
Note:Activated is the folder name that contains the angular code.
4.you place your url link:
let link = "dev/Groups/{groupId}" (just a sample)`enter code here`

5.npm start to restart you application.

答案 1 :(得分:1)

这是因为您必须确保服务器配置为针对OPTIONS请求发送200或204,该请求由浏览器在PUT请求之前触发。

查看第一个回复here

使用PUT进行此操作的原因是,它会触发OPTIONS(浏览器的附加预检请求),以查看有关哪些方法可以触发此操作的更多信息,您可以查看here。基本上,浏览器是在PUT之前“ ping”服务器。

令人沮丧的是,这似乎是一个前端问题,因为在测试(例如,如果您使用邮递员)时PUT请求可以工作,但浏览器却不能工作。

所以这不是前端问题,无法使用Angular解决。