jQuery AJAX调用上的POST数组为空

编程入门 行业动态 更新时间:2024-10-27 17:10:20
本文介绍了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数组为空

本文发布于:2023-10-11 17:51:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   为空   jQuery   AJAX   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!