如何在角度最新的cli版本

时间:2018-01-04 15:11:08

标签: angular-cli

在最新版本中进行CORS操作的正确生产方式是什么?

我的功能

 public fetchData() {
    console.log("GET WITH HEADERS");


    return this.http.get('http://192.168.31.73:8080/myjersey/rest/hello/dd').map(
      (response) => response
    )
      .subscribe(
      (data) => console.log(data)
      )
  }

  public authentication(email, password) {
    console.log("POST WITH HEADERS");
    const body = { name: 'Brad' };

    this.http
      .post('http://192.168.31.73:8080/myjersey/rest/hello/dd', {
        email: email,
        password: password

      }, {
        headers: new HttpHeaders().set('Access-Control-Allow-Origin', '*')
          .set('Access-Control-Allow-Credentials', 'true')
          .set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE')
          .set('Access-Control-Allow-Headers', 'X-Custom-Header'),
      })
      .subscribe();


  }
下面的

是我得到的例外:

Failed to load http://192.168.31.73:8080/myjersey/rest/hello/dd: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

不知怎的,我的Http Get正在工作,但没有POST.Please建议在这种情况下我应该做什么我在请求和响应中添加了Access-Control-Allow-Origin仍然会收到CORS问题。

PS:我不懂PHP。我正在使用带角度的泽西休息服务。

this is response body from server this is response header from server

1 个答案:

答案 0 :(得分:-2)

我认为你可以轻松解决它。

请在浏览器中设置cors扩展名。 How to install the chrome extention

必须在服务器端解决。