HttpClient Angular CORS Twilio

倾然丶 夕夏残阳落幕 提交于 2019-12-11 21:11:35

问题


I'm trying to do a "GET" request to Twilio API using HttpClient module

import { HttpClient } from '@angular/common/http';

I'm Using

Angular CLI: 6.0.8
Node: 8.11.3
OS: darwin x64

I'm testing using Firefox and Chrome, getting the same error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://api.authy.com/protected/json/phones/verification/check?api_key=****. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

This is the function I'm running to test

    submitCode() {
    let data = {
        api_key: '',
        verification_code: '',
        phone_number: '',
        country_code:''
    }
    let headers = {
        'Access-Control-Allow-Origin' : '*',
        'Content-Type' : 'application/json',
    }
    this.HTTP.get('http://api.authy.com/protected/json/phones/verification/check',{params: data,headers:headers}).subscribe(
        resp=>{alert(1)},
        err=>{alert(2)},
        ()=>{alert(3)}
    )
}

What happens is that the second alert triggers ?

I tried using it without headers, same issue. However, when I leave headers empty, the API gets triggered, and it shows by Twilio monitors, but no data returned because of CORS.


回答1:


Solved this issue by deploying a backend server and sending requests to that.

(Better than sending from the actual client and displaying your API Key in your requests)

Other solution is to use CORS-Anywhere http://cors-anywhere.herokuapp.com/



来源:https://stackoverflow.com/questions/51606925/httpclient-angular-cors-twilio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!