Axios 没有 运行 - React JS
Axios does not run - React JS
我正在使用 axios
通过 React JS 发出 POST 请求,特别是对 Sendinblue API 的 POST 请求,发现 here。我使用 axios
尝试了多种变体,但是我没有成功完成 POST 请求。
代码如下:
var email = this.state.value;
var data = JSON.stringify({ "listIds": [5], "email": email, "attributes": { "SMS": "sms-Number" }, "emailBlacklisted": false, "smsBlacklisted": false, "updateEnabled": true });
var config = {
method: 'post',
url: 'https://api.sendinblue.com/v3/contacts',
headers: {
'api-key': 'API-key',
'Content-Type': 'application/json'
},
data: data
};
axios(config)
.then(function (response) {
alert(JSON.stringify(response.data));
})
.catch(function (error) {
alert(error);
});
我已经在顶部导入了 axios
和所需的库。
鉴于此代码,我在执行 axios 时从未收到警报。也就是说,我永远不会收到成功或错误的警报。所以,它似乎甚至不是 运行 axios
部分。
关于为什么会这样有什么想法吗?谢谢!
感谢大家的帮助。我能够通过将触发此代码的按钮放在 form
标记之外来解决此错误。
旧代码(无效):
<form>
<TextInput></TextInput>
<button onClick={this.runCode}>Click here</button>
</form>
新代码(有效):
<form>
<TextInput></TextInput>
</form>
<button onClick={this.runCode}>Click here</button>
我正在使用 axios
通过 React JS 发出 POST 请求,特别是对 Sendinblue API 的 POST 请求,发现 here。我使用 axios
尝试了多种变体,但是我没有成功完成 POST 请求。
代码如下:
var email = this.state.value;
var data = JSON.stringify({ "listIds": [5], "email": email, "attributes": { "SMS": "sms-Number" }, "emailBlacklisted": false, "smsBlacklisted": false, "updateEnabled": true });
var config = {
method: 'post',
url: 'https://api.sendinblue.com/v3/contacts',
headers: {
'api-key': 'API-key',
'Content-Type': 'application/json'
},
data: data
};
axios(config)
.then(function (response) {
alert(JSON.stringify(response.data));
})
.catch(function (error) {
alert(error);
});
我已经在顶部导入了 axios
和所需的库。
鉴于此代码,我在执行 axios 时从未收到警报。也就是说,我永远不会收到成功或错误的警报。所以,它似乎甚至不是 运行 axios
部分。
关于为什么会这样有什么想法吗?谢谢!
感谢大家的帮助。我能够通过将触发此代码的按钮放在 form
标记之外来解决此错误。
旧代码(无效):
<form>
<TextInput></TextInput>
<button onClick={this.runCode}>Click here</button>
</form>
新代码(有效):
<form>
<TextInput></TextInput>
</form>
<button onClick={this.runCode}>Click here</button>