【发布时间】:2017-12-05 01:32:31
【问题描述】:
我是 ReactJS 的新手,我正在尝试构建这个需要使用 mailchimp 的应用程序,以便用户可以订阅时事通讯。我需要使用 axios 发出请求吗?有人可以指导我吗?我在哪里放我的 api 密钥?我在下面的代码中做对了吗?我将我的 mailchimps 用户名放在“用户名”中,将我的 apikey 放在“xxxxxxxxxxxxxxxxxxx-us16”中,但是,我收到 401 错误说未经授权,但我的控制台确实说 Fetch Complete: POST 并且没有发现任何错误。
import React, {Component} from 'react';
import './Subscribe.css';
class Subscribe extends Component {
sub = () => {
let authenticationString = btoa('username:xxxxxxxxxxxxxxxxxxx-us16');
authenticationString = "Basic " + authenticationString;
fetch('https://us16.api.mailchimp.com/3.0/lists/xxxxxxxxx/members', {
mode: 'no-cors',
method: 'POST',
headers: {
'Authorization': authenticationString,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email_address: "somedude@gmail.com",
status: "subscribed",
})
}).then(function(e){
console.log('complete')
}).catch(function(e){
console.log("fetch error");
})
};
render () {
return(
<div>
<button onClick={this.sub}> subscribe </button>
</div>
);
};
};
【问题讨论】:
-
你能解释一下你什么时候想提出请求吗?并为您的问题提供一些示例。
-
请再检查一遍
-
您能否也发布来自 mailchimp 的确切错误消息?
标签: javascript reactjs mailchimp