gpt4 book ai didi

javascript - React - WP_Mail 可与 jQuery 配合使用,但不能与 axios 配合使用

转载 作者:行者123 更新时间:2023-12-02 21:32:09 25 4
gpt4 key购买 nike

我的 React 应用程序中有一个联系表单,我正在尝试让该表单使用 WP_Mail 发送电子邮件。我正在尝试使用 Axios 来做到这一点,但我似乎无法让它工作。我在 admin-ajax 调用中收到 400 错误。

axios.post(url, {
action: 'send_form',
data: {
email: 'test@test.com',
message: 'testing'
}
})
.then(function (response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});

我可以使用 jQuery 让它工作,但如果可能的话我宁愿不必使用它:

$.ajax({
url: url,
dataType: 'json',
data: {action: 'send_form', email: 'test@test.com'},
cache: false,
success: function(data) {
console.log(data);
}.bind(this),
error: function(xhr, status, err) {
alert(err.toString());
}.bind(this)
});

我认为问题与有效负载 header 的格式有关。当我使用 Axios 时,数据以 JSON 负载 header 的形式传递,但当我使用 jQuery 时,数据以查询字符串参数的形式传递。

最佳答案

你能尝试一下吗:

let form_data = new FormData;
form_data.append('action', 'send_form');
form_data.append('email', 'test@test.com');
form_data.append('message', 'testing');

axios.post(url, {
form_data
})
.then(function (response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});

关于javascript - React - WP_Mail 可与 jQuery 配合使用,但不能与 axios 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60595184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com