jQuery AJAX调用上的POST数组为空 [英] POST array empty on jQuery AJAX call

查看:254
本文介绍了jQuery AJAX调用上的POST数组为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想问一下,因为与此主题相关的其他文章很多(一个两个),但这些帖子中没有一个解决方案申请我.

I hesitate to ask, as there are numerous other posts on this topic (one and two for example), but none of the solutions in those posts seem to apply to me.

我正在尝试将JSON编码的对象从页面传递到PHP控制器,然后以一些信息作为响应.

I am trying to pass a JSON-encoded object from a page to a PHP controller, and then respond back with some information.

如果我在Firebug中观看,我可以在"Post"选项卡下看到正在发送的对象,但是当我打印$ _GET,$ _ POST和$ _REQUEST数组时,关于json对象什么也看不到. $ _GET数组至少显示我的'update-player'查询字符串,但是POST为空,而REQUEST仅显示我拥有的一些本地cookie.

If I watch in Firebug, I can see the object being sent under the 'Post' tab, however when I print out the $_GET, $_POST, and $_REQUEST arrays I see nothing at all regarding the json object. The $_GET array at least shows my querystring of 'update-player', however POST is empty and REQUEST only shows some local cookies I have.

这是我的jQuery代码.如您所见,我目前正在对JSON进行硬编码,目的是我将拥有一个jQuery方法来更新本地对象.

Here is my jQuery code. As you can see I'm hardcoding the JSON at the moment, and the intention is that I will have a jQuery method updating the local object.

function sendPlayerUpdate(row, col) {
    var playerinfo = [
        {
          "id": 1,
          "row": row,
          "col": col
        }
      ];

        alert(playerinfo[0].id); //debugging

    $.ajax({
        type: 'POST',
        url:"controller.php?a=update-player",
        //data: $.toJSON(playerinfo[0],
        data: { json: JSON.stringify(playerinfo) },
        contentType: "application/json",
        success: function (){

        },
        dataType: 'json'
      });
 };

我处理请求的相应PHP代码:

My corresponding PHP code handling the request:

// update player information from AJAX POST
case "update-player":
  if (isset($_POST['json'])) echo "json received\n\n";
  else echo "json not received\n\n";
  echo "GET VARIABLES\n";
  print_r($_GET);
  echo "\n\nPOST VARIABLES\n";
  print_r($_POST);
  echo "\n\nREQUEST VARIABLES\n";
  print_r($_REQUEST);

而且,我在Firebug中看到的是

And, what I see in Firebug:

没收到json

json not received

GET VARIABLES
Array
(
    [a] => update-player
)


POST VARIABLES
Array
(
)


REQUEST VARIABLES
Array
(
    [a] => update-player
    (local cookies)
)

推荐答案

按如下所示在PHP中尝试(当请求是application/json时,则不会将数据放入$ _POST)

Try in PHP as below (When request is an application/json, then you will not get data into $_POST)

var_dump(json_decode(file_get_contents("php://input")));

这篇关于jQuery AJAX调用上的POST数组为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆