Zendesk API + Angular:由于 CORS,无法接收数据

Zendesk API + Angular: Cannot receive data due to CORS

我正在使用 Zendesk API and am able to make post requests (even though the response tells me there is an error) but I cannot make GET requests using my Angular 4 app and HttpClient

searchZendesk( query : string ) {
    console.log(query)

    this.search( query ).subscribe(
      (data) => console.log(data),
      (err) => console.log(err.error),
      () => console.log("complete")
    )
}

search( query : string ) {
    let headers = this.setOptions()
    let url = `${ this.baseUrl }/search.json?query=${ query }`

    return this.http.get(`${ this.baseUrl }/search.json?query=${ query }`, { headers: headers })
  }

setOptions() {
    let key = this.setKey()
    let headers = new HttpHeaders({
      'Authorization': key,
    })

    return headers
}

调用 searchZendesk() 后,我收到以下错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

您需要将 Zendesk 和 angular 代码托管在同一个域中,以避免 CORS 问题。

您可以完全控制如何部署您的 js 代码(angular 应用程序),所以我把它留给您。

要在同一域上托管 zendesk,请按照本文进行操作:

https://support.zendesk.com/hc/en-us/articles/203664356-Changing-the-address-of-your-Help-Center-subdomain-host-mapping-

——

此外,按照本文 https://develop.zendesk.com/hc/en-us/articles/360001074268-Making-cross-origin-browser-side-API-requests 如果您使用 oAuth 身份验证,您应该能够从客户端访问大多数 API 而不会出现 CORS 问题!

If an API request is authenticated with OAuth :

Zendesk 在响应中包含一个特殊的 "Access-Control-Allow-Origin" CORS header。 header 的值为“*”,允许来自任何来源的页面的请求。 header 基本上授予浏览器访问 Zendesk 域中资源的权限。