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 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失