gpt4 book ai didi

javascript - Ajax 是不是将变量传递给另一个页面?

转载 作者:行者123 更新时间:2023-12-02 06:27:10 26 4
gpt4 key购买 nike

我想在用户点击订阅按钮时将他的 email 保存到数据库中。但是当我输入电子邮件并单击订阅按钮时,它传递了一个空数组!

订阅表单代码:

<form id="cx-subscribe-user">
<input type="text" name="email" id="email" placeholder="Enter Your Email" />
<button type="submit" id="subscribe-button">Subscribe</button>
</form>

js代码:

$('#cx-subscribe-user').on('click', function (e) {
e.preventDefault();
var email = $('#email').val();
$.ajax({
type: "GET",
url: "subscribe-the-user.php",
processData: false,
dataType: 'json',
data: {email: email},
success: function(data) {
console.log('saved scucess');
}
});
});

subscribe-the-user.php 文件中,我尝试对请求进行 var_dump,但它打印的是空数组:

var_dump($_GET);exit;

var_dump 我收到这个空数组数组(0){}

最佳答案

删除 processData,如文档所述,它会弄乱您的请求

processData (default: true)

Type: Boolean

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false. https://api.jquery.com/jQuery.ajax/

关于javascript - Ajax 是不是将变量传递给另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55630300/

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