【问题标题】:Can't decode received JSON to array无法将收到的 JSON 解码为数组
【发布时间】:2016-09-10 05:53:31
【问题描述】:

JS

序列化 FORM 中的数据,将其字符串化并使用 AJAX 将其作为 JSON 发布到 update.php

jQuery.fn.serializeObject = function () {
  var formData = {};
  var formArray = this.serializeArray();

  for(var i = 0, n = formArray.length; i < n; ++i)
    formData[formArray[i].name] = formArray[i].value;

  return formData;
};

$(function() {
    $('form').submit(function() {
        data = $('form').serializeObject();
        alert(JSON.stringify(data));   
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'inc/update.php',
            data: {json: JSON.stringify(data)},
            dataType: 'json'
        });  
    });
}); 

update.php 文件应解码为数组

$str_json = file_get_contents('php://input'); //($_POST doesn't work here)
$response = json_decode($str_json, true); // decoding received JSON to array

$name = $response['name'];

$update = $pdo->prepare("UPDATE user SET name='".$name."' WHERE id='3';");
$update->execute();//the SQL works fine with String for $name

使用 Firefox 中的 Tamper Data 插件,我检查了 POSTDATA,它是:

json=%7B%22name%22%3A%22fff%22%7D

这就像:

json={"name":"fff"}

我是 JS/AJAX/JSON 的新手,我找不到我的错误。所以请帮助我。
我搜索了好几个小时都没有成功。

【问题讨论】:

  • 第二个解码参数 = true
  • -我试过 $name = $response['name'];但不起作用:-( -2nd decode 参数已经是真的
  • 我只是想指出您已将您的第二个参数设置为 true
  • 现在我明白了,谢谢 :-)

标签: javascript php json ajax post


【解决方案1】:

不知道写serializeObject函数有什么意义,什么时候可以用serializeArray

Javascript:

$(function() {
    $('form').submit(function(e) {
        e.preventDefault(); // Stop normal submission, which is probably why your PHP code isn't working
        data = $('form').serializeArray();
        alert(JSON.stringify(data));   
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'inc/update.php',
            data: {
                json: JSON.stringify(data)
            },
            dataType: 'json'
        });  
    });
    return false;
});

PHP:

$str_json = _$_POST['json'];
$response = json_decode($str_json, true); // decoding received JSON to array
$name = $response['name'];

如果你在json_decode 中使用true 作为第二个参数,它返回一个数组而不是对象。 所以你需要这样做

$name = $response['name'];

【讨论】:

  • 我已经根据这些代码交换了我的代码。我不知道serializeArray。发布的 json 现在看起来像 [{"name":"name","value":"testvalue"}]。但我无法解码。
  • @Luk4s error_reporting(E_ALL); 在您的 php 文件顶部,然后检查响应。
  • alert(xhr.status);//200 alert(thrownError);//SyntaxError: Unexpected token
  • @Luk4s 查询是否更新了表中的行?您是否在 PHP 脚本末尾回显 JSON?
【解决方案2】:

我在我们的帮助下发现了问题:JSON-String 不正确

一步一步解释:

$str_json = file_get_contents('php://input');
echo $str_json;//json%3D%5B%7B%22name%22%3A%22Max%22%2C%22value%22%3A%22testvalue%22%7D%5D

$str_json = urldecode($str_json);
echo $str_json;//json=[{"name":"Max","value":"testvalue"}]

$str_json = str_replace('json=', '', $str_json);
echo $str_json;//[{"name":"Max","value":"testvalue"}]

//now it is a json-string which can be json_decoded
$arr_json = json_decode($str_json, true);
$name = $arr_json['name'];
echo $name;//Max

非常感谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多