MAO-EYE 2016-03-07 00:11 采纳率: 100%
浏览 132

$ _POST数组为空

I am working with XAMPP and PHPStorm v9, running locally, a simple application that does an AJAX callback.

My server handler for the AJAX request is showing an empty $_POST array and I have no idea why. Before I take all of this code and run on a different server, to eliminate the platform as the issue, does anyone see why the $_POST array would be empty? Any suggestions? Thanks

The XHR pane, shows the post executed. enter image description here

The JS

if (valid) {

    var queryStr = 'fName=' + firstName.val() + '&lName=' + lastName.val() + '&email=' + email.val() + '&phone=' + phone.val();

    var f = firstName.val();
    var l = lastName.val();
    var e = email.val();
    var p = phone.val();

    $.ajax({
        type: "POST",
        url: '/ACS567-Projects/addContact.php',
        data: {
            fName: f,
            lName: l,
            email: e,
            phone: p
        },
        dataType: 'html',
        success: OnAddSuccess,
        error: OnAddFailure,
        timeout: 60000
    });
}

The PHP handler

<?php

if(isset($_POST['fName'])) {

    $firstName = $_POST['fName'];
    $lastName = $_POST['lName'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $addressBook->addContact($firstName,$lastName,$email,$phone);

    echo "<b>success</b>";
    exit();
}

echo "<b>failure</b>";
exit();
  • 写回答

1条回答 默认 最新

  • weixin_33704591 2016-03-07 00:25
    关注

    Data will not automatically turn into an array ready for use in your PHP script. Read the jQuery documentation:

    It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

    Also, you should use 'method' instead of 'type' if you don't have an old jQuery version.

    More info on the .ajax function found here:

    http://api.jquery.com/jquery.ajax/

    Have you inspected your client/browser to see what is posted and dumped the $_POST variable to see that it's really empty?

    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条