【问题标题】:Unable to send additional data through $.post()无法通过 $.post() 发送其他数据
【发布时间】:2015-08-19 01:37:32
【问题描述】:

使用 $.post(),我试图将 $.cookie('pear') 的值发送到我的 PHP 表单,它会用它做一些事情。但问题是,PHP 表单似乎无法接收$.cookie('pear') 的值。

需要明确的是,$.cookie('pear') 绝对有价值。我做了一个alert($.cookie('pear')) 仔细检查,它肯定显示了$.cookie('pear') 的值。

在下面查看我的代码。感谢您指出它有什么问题的帮助。也许我的 $.post() 实现有问题?

jQuery

submit_fruit.on({

    click: function () {

      $('#market').click();

      var pears = $.cookie('pears')

      alert(pears) // this works. it alert the value of the cookie 'pears'

      $.post('fruitsubmission.php', { pears: pears }, function(data) {

    }); // but this troublesome piece of $.post() doesn't seem to send 'pears' over to the PHP form.

}

});

PHP

$fruit = $_POST['pears'];

echo $fruit;

-- some mySQL data submissions stuff here --

但我无法获得从 $.post() 调用发送过来的值 'pears'。

错误报告

Notice: Undefined index: pears in ******* on line 10

【问题讨论】:

  • 为什么在$.post('#fruitsubmission.php', ... 中使用# 字符?像这样使用它具有发布到发起请求的页面的效果。这是你的意图吗?
  • 抱歉,这是我的疏忽。我的意思是没有标签。不过,我仍然收到相同的错误报告。
  • 如果你设置var pears = "this is a test";你会得到同样的错误吗?

标签: jquery cookies http-post postback


【解决方案1】:

当我将 pears 设置为空数组或空对象时,我能够重现 PHP Notice: Undefined index

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(document).ready(function() {
    // var pears = {};
    var pears = new Array();
    $.post('fruitsubmission.php', { pears: pears }, function(data) {
        alert(data);
    });
});
</script>

当我将其设置为 var pears = "this is a test"; 之类的内容时,它会按预期工作,因此您遇到的错误可能是由脚本的其他部分引起的。

尝试在您的环境中隔离此错误,您可以从上面的脚本开始,然后添加其他元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 2017-01-19
    相关资源
    最近更新 更多