douqi2571 2012-11-05 19:58 采纳率: 0%
浏览 34
已采纳

ajax发布后的空php变量

I have the following code in my script file:

$.ajax({
    url: "server.php?saveEvent",
    data: "myEvent=" + JSON.stringify(myEvent),
    dataType: "json",
    type: "post",
    success: function (data) {
        if (data) {
            $("#log").append("<br />Evenement saved.");
        }
    }
});

In server.php I retrieve the variable using:

if (isset($_GET['saveEvent'])) {
    if (isset($_POST['myEvent'])) {
        $firephp->log(gettype($_POST['myEvent']));
        $myEvent = json_decode($_POST['myEvent'], true);
    }
}

When I tested this on my localhost, everything went fine. Unfortunately, after deployment, $myEvent was empty.

Using firephp, I tested what was in the variable, and I looked at the headers being sent. The object was sent to the server, but still somehow php see's it as being an empty variable.

Any ideas on how this is possible? Is it a php version or json issue?

EDIT: PHP ver= 5.2.17 / json enabled

EDIT2: Changing te url to ?saveEvent=1 didnt change anything

Edit3: I realize making a get and post is a bit strange, I'll try changing that, but get/post shouldn't be a problem I think

  • 写回答

3条回答 默认 最新

  • dongxiong2000 2012-11-05 20:06
    关注

    Your uri contains a GET parameter, saveEvent. You're checking for POST data only

    try checking this: $_GET['saveEvent']

    Even so, that parameter hasn't got a value assigned to it, perhaps change the url to ?saveEvent=1.
    There's also no reason for you to use 2 if statements:

    if (isset($_GET['saveEvent'] && isset($_POST['myEvent']))
    {
        //do stuff
    }
    

    Since it seems there's more going on than just the GET vs. POST issue, you might want to add an ampersand (&) at the end of your url: look at your console, the XHR request just pastes the POST parameters at the end of your url, so the uri will look like either one of the following urls:

    server.php?saveEvent=truemyEvent=foo
    server.php?saveEvent=1myEvent=foo
    server.php?saveEventmyEvent=foo
    //or
    server.php?saveEvent=1?myEvent=foo
    

    Whereas, what you need is:

        server.php?saveEvent=1&myEvent=foo&something=else
    

    Basically, stick to 1 method, either POST or GET, and make sure that the various parameters are separated as they're supposed to be separated.
    I'm not sure if this is the issue, but try var_dump-ing the $_REQUEST super-global, along with $_GET, $_POST and what have you...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题