【问题标题】:passing JavaScript object to PHP not working将 JavaScript 对象传递给 PHP 不起作用
【发布时间】:2016-04-19 00:15:56
【问题描述】:

我正在尝试通过 JSON.stringify() 将我的 javascript 对象发送到 PHP

Javascript:

$('#save').on('click touch', function(){
    obj = {
        "1" : {
            "1" : "hey",
            "2" : "hay"
            },              
        "2" : {
            "1" : "hey",
            "2" : "hay"
            }
    }

    var json = JSON.stringify( obj );
    console.log(json)
    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        success: function(data) {
            alert(data);
            $("p").text(data);
        }
    });
});

ajax.php:

<?php 
    $obj = json_decode($json);
    echo $obj;
?> 

但此代码返回一个错误,指出 $json 未定义。 我不知道为什么这不起作用。

【问题讨论】:

  • 您尚未在$.ajax 选项上设置data 属性。
  • POST 数据将在 PHP 中的 $_POST 变量中可用
  • @csum: 仅当 OP 真的费心告诉 jquery USE ajax 调用中的 json var ...
  • @MarcB 是的,是的。这更像是一个笼统的陈述。 POST 数据在$_POST
  • 我已经解决了这个问题。谢谢大家

标签: javascript php json ajax object


【解决方案1】:

用这个替换你的 ajax 代码。

$.ajax({
    type: 'POST',
    url: 'ajax.php',
    data: json
    success: function(data) {
        alert(data);
        $("p").text(data);
    }
});

对于ajax php

<?php 
    $obj = json_decode($_POST['data']);
    echo $obj;
?> 

【讨论】:

    【解决方案2】:

    有两个问题。

    1. 您没有随请求发送任何数据
    2. 这不是您从 PHP 中的请求中获取值的方式

    首先,添加这个*:

    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data : { json: json }, // <---------------------
        ...
    

    * 之所以有效,是因为 jQuery 实现会自动将任何非字符串数据参数转换为表单 urlencoded 查询字符串。见docs

    然后,在你的 PHP 中,你应该这样做:

    $jsonStr = $_POST['json'];
    $json = json_decode($jsonStr);
    

    编辑:

    另一种可能的方式:

    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data : json , // <---------------------
        ...
    

    这样,您的数据将不是有效的 form-urlencoded 输入,因此 PHP 不会将其解析为 $_POST,但您仍然可以通过以下方式获取输入的内容:

    $jsonStr = file_get_contents("php://input");
    $json = json_decode($jsonStr);
    

    【讨论】:

    • 您能解释一下第二个选项不是有效的form-urlencoded 输入背后的原因吗?是因为json 变量的内容吗?键是数字?
    • form-urlencoded 值的格式为 param1=value1&amp;param2=value2&amp;param3=value3...
    • 基本上就是一个查询字符串。
    • 那么将你的 var 包裹在 {json: ___ } 中可以解决这个问题吗?
    • 解决了因为jQuery 会自动将任何对象转换为form-urlencoded 字符串。可能不适用于其他 Ajax 库。
    【解决方案3】:

    在此处查看参考jQuery ajax

    数据参数:指定要发送到服务器的数据。

    试试这个:

    $('#save').on('click touch', function(){
            obj = {
           "1" : {
               "1" : "hey",
               "2" : "hay"
            },              
        "2" : {
            "1" : "hey",
            "2" : "hay"
            }
    }
    
    var json = JSON.stringify( obj );
    
    $.ajax({
        data : json,
        type: 'POST',
        url: 'ajax.php',
        success: function(data) {
            alert(data);
            $("p").text(data);
        }
    });
    });
    

    【讨论】:

    • dataType 指的是预期的响应,不是请求。
    【解决方案4】:

    您必须使用 ajax 请求发送 obj

     $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data : json,
        dataType : 'json' // for json response
        ...
    

    【讨论】:

    • dataType 指的是预期的响应,没有请求。
    • @HenriqueBarcelos 在评论中说'for json response'
    【解决方案5】:

    嗯 - 你永远不会在 AJAX 请求中传递你的数据!

    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data: json //<---- RIGHT HERE
        success: function(data) {
            alert(data);
            $("p").text(data);
        }
    });
    

    【讨论】:

    • 问题解决了一半
    • 此外,除非 OP 有 register_globals=On,否则 OP 应该分配 $json = $_POST['json']
    • 别忘了json后面的逗号
    • @HenriqueBarcelos:呃,php 不知道 json 是什么。 json 字符串只是 php 的纯文本字符串。 php 在 POST 中期望 key=value 对,value 本身完全没有意义。 register_globals 从来没有,也永远不会解码 json...
    • 我的意思是这部分:$obj = json_decode($json)。如果register_globals=On 并且数据在'json' 键下传递,例如json={"foo":"bar"},它会起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2016-04-02
    • 2017-10-07
    相关资源
    最近更新 更多