Reactjs - Axios - Nexmo POST请求无效(No'Access-Control-Allow-Origin')

时间:2017-04-03 19:39:30

标签: reactjs post access-control axios nexmo

我正在使用Reactjs,我正在尝试使用Axios发送一个POST请求,以便用Nexmo发送短信。我可以收到短信,但我在控制台上出现此错误请求的资源上没有“Access-Control-Allow-Origin”标头。因此,“http://localhost:3000”来源不允许访问。 这是我的代码:

axios({
  method : 'post',
  url : 'https://rest.nexmo.com/sms/json',
  params:{
    api_key:'xxxxxxxxx',
    api_secret:'xxxxxxxxx',
    to:phoneNumber,
    from:'NEXMO',
    text:"New message"
  },
  headers:{
      'Content-Type': 'application/x-www-form-urlencoded'
  }
})
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
});

如何解决此问题? THX

1 个答案:

答案 0 :(得分:1)

Nexmo SMS API只应与受信任的安全客户端进行交互。使用API​​密钥和秘密凭据,您可以访问Nexmo帐户,因此不应将这些凭据公开给应用程序的“用户”。这通常意味着您只应使用来自服务器的API的密钥和机密凭证。

Nexmo Voice API确实提供了更适合客户端API交互的JWT auth支持,因为您可以创建非常短暂的令牌并控制令牌允许的资源和功能。但SMS API仅提供密钥和秘密身份验证。