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

标签 javascript php arrays ajax

我想在用户点击订阅按钮时将他的 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/

相关文章:

javascript - 查询 Firebase 数据库但未获取数据或错误

asp.net - jQuery 和 Colorbox 的问题。动态 href 未反射(reflect)第一次出现后的变化

php - 更新多行sql语句

C:带有 2D 阵列的 SEGFAULT

python - 我可以从函数参数中拆分一个数组吗

javascript - Highcharts - 图表下的位置标题

php - 与 php 集成的 wkhtmltopdf 在 Centos 上不起作用(拒绝访问)

php - 无法从 .htaccess 生成的 url 获取变量

JavaScript Array、Stack、Queue——这种特定 API 设计背后的动机是什么?

javascript - map 'undefined' 的 Google map /Javascript 问题