如何从PHP中的serializeArray获取POST值

如何从PHP中的serializeArray获取POST值

本文介绍了如何从PHP中的serializeArray获取POST值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这种新方法,见过serializeArray().

I am trying this new method I've seen serializeArray().

//with ajax
var data = $("#form :input").serializeArray();
post_var = {'action': 'process', 'data': data };
$.ajax({.....etc

所以我得到了这些键值对,但是如何使用PHP访问它们呢?

So I get these key value pairs, but how do I access them with PHP?

我以为我需要这样做,但是它行不通:

I thought I needed to do this, but it won't work:

// in PHP script
$data = json_decode($_POST['data'], true);

var_dump($data);// will return NULL?

谢谢理查德

推荐答案

就像Gumbo建议的那样,您可能不处理 json_decode .
试试

Like Gumbo suggested, you are likely not processing the return value of json_decode.
Try

$data = json_decode($_POST['data'], true);
var_dump($data);

如果$data不包含预期的数据,则var_dump($_POST);查看Ajax调用将哪些内容发布到了脚本中.可能是您试图从错误的密钥访问JSON.

If $data does not contain the expected data, then var_dump($_POST); to see what the Ajax call did post to your script. Might be you are trying to access the JSON from the wrong key.

编辑
实际上,您应该首先确保真正发送JSON :)
序列化状态的jQuery文档..serializeArray()方法创建对象的JavaScript 数组准备好编码为JSON字符串.要编码的不是JSON.显然,jQuery中没有Object2JSON函数,因此请使用 https://github .com/douglascrockford/JSON-js/blob/master/json2.js 作为第三方库或使用 http://api.jquery.com/serialize/.

EDIT
Actually, you should make sure that you are really sending JSON in the first place :)
The jQuery docs for serialize state The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. Ready to be encoded is not JSON. Apparently, there is no Object2JSON function in jQuery so either use https://github.com/douglascrockford/JSON-js/blob/master/json2.js as a 3rd party lib or use http://api.jquery.com/serialize/ instead.

这篇关于如何从PHP中的serializeArray获取POST值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:10