ds2321 2014-04-02 13:27
浏览 48
已采纳

JSON PHP解码无法正常工作

I have seen many examples, but for whatever reason, none seem to work for me.

I have the following sent from a app, via ajax, to a php file. This is how it looks when its sent:



    obj:{"ClientData":
    [{
        "firstName":"Master",
        "lastName":"Tester",
        "email":"me@me.com",
        "dob":"1973-01-22",
        "age":"51",
    }],
    "HealthData":
    [
        "condition : Prone to Fainting / Dizziness",
        "condition : Allergic Response to Plasters",
    ],
    "someData":
    [{
        "firstName":"Male",
        "lastName":"checking",
    }]
    }

Here is how it looks in debugger

Code as is:

{"ClientData":[{"firstName":"Master","lastName":"Tester","email":"me@me.com","dob":"1973-01-22","age":"51","pierceType":"Vici","street":"number of house","city":"here","county":"there","postcode":"everywhere"}],"HealthData":[["condtion : Prone to Fainting / Dizziness","condtion : Allergic Response to Plasters","condtion : Prone to Fainting / Dizziness"]],"PiercerData":[{"firstName":"Male","lastName":"checking","pierceDate":"2013-02-25","jewelleryType":"Vici","jewelleryDesign":"Vidi","jewellerySize":"Vici","idChecked":null,"medicalChecked":null,"notes":"This is for more info"}]}

This comes in one long line into a php file, here is the code:

<?php
header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");
//var_dump($_POST['obj']);

$Ojb = json_decode($_POST['obj'],true);

$clientData = $Ojb['ClientData'];
$healthData = $Ojb->HealthData;
$someData = $Ojb->someData;

print_r($clientData['firstName']);    
?>

No matter what I have tried, I am unable to see any of the information, I don't even get an error, just blank! Please can someone point me in the right direction.

Thank you :)

UPDATE

Here is the code that creates the object:

ClientObject = {

        ClientData : [
            {
                firstName : localStorage.getItem('cfn'),
                lastName : localStorage.getItem('cln'),
                email : localStorage.getItem('cem'),
                dob : localStorage.getItem('cdo'),
                age : localStorage.getItem('cag'),
                pierceType : localStorage.getItem('cpt'),
                street : localStorage.getItem('cst'),
                city : localStorage.getItem('cci'),
                county : localStorage.getItem('cco'),
                postcode : localStorage.getItem('cpc')
            }
        ],

        HealthData : health,

        PiercerData : [
        {
                firstName : localStorage.getItem('pfn'),
                lastName : localStorage.getItem('pln'),
                pierceDate : localStorage.getItem('pda'),
                jewelleryType : localStorage.getItem('pjt'),
                jewelleryDesign : localStorage.getItem('pjd'),
                jewellerySize : localStorage.getItem('pjs'),
                idChecked: localStorage.getItem('pid'),
                medicalChecked: localStorage.getItem('pmh'),
                notes: localStorage.getItem('poi')
        }
        ]

    };

And here is how its sent:

function senddata() {
    $.ajax({
        url: 'http://domain.com/app.php',
        type: 'POST',
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        dataType: 'jsonp',              
        data: 'obj='+JSON.stringify(ClientObject),

        success : function(res) {
            console.log(res);

        },
        error: function(err) {

        }
    });
}
  • 写回答

2条回答 默认 最新

  • dongluan7821 2014-04-02 14:13
    关注

    There are a few things that will cause problems:

    1. why dataType: 'jsonp'? If you don't intend to utilize jsonp, don't instruct jQuery to do this. See the docs: https://api.jquery.com/jQuery.ajax/

      "jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

    2. 'obj='+JSON.stringify(ClientObject), this will guarantee invalid json.

    For reference, have a look at this question: jQuery ajax, how to send JSON instead of QueryString on how to send json with jquery.


    That said, try the following:

    function senddata() {
      $.ajax({
        url: 'app.php',
        type: 'POST',
        crossDomain: true,
        contentType: 'application/json; charset=utf-8"',
        data: JSON.stringify(ClientObject),
        success : function(res) {
          console.log(res);
        },
        error: function(err) {
        }
      });
    }
    

    And in app.php use

    $input = json_decode(file_get_contents('php://input'));
    

    to get the data. Use it like:

    var_dump($input->ClientData[0]->firstName); // => string(6) "Master"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。