[英]JSON PHP decode not working
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. 我有以下内容通过ajax从应用程序发送到php文件。 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", }] }
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文件,这是代码:
<?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) {
}
});
}
$Ojb = json_decode($_POST['obj'],true);
makes it array so u need to get them using array index instead of object 使它成为数组,所以您需要使用数组索引而不是对象来获取它们
UPDATE1 更新1
With your update here how it could be done 在这里进行更新
$str ='{"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"}]}' ;
$obj = json_decode($str,true);
echo $obj["ClientData"][0]["firstName"];
You can get other elements as above 您可以获取上述其他元素
UPDATE2 更新2
You are sending the data as JSONP and this will make the request as 您正在以JSONP的形式发送数据,这将以
?callback=jQuery17108448240196903967_1396448041102&{"ClientData"
Now you are also adding data: 'obj='
which is not correct. 现在,您还要添加
data: 'obj='
,这是不正确的。
You can simply send as json not jsonp 您可以简单地以json而不是jsonp发送
and on the php file you can do as 并在php文件上,您可以执行以下操作
$Ojb = json_decode(file_get_contents('php://input'),true);
There are a few things that will cause problems: 有几件事会引起问题:
why dataType: 'jsonp'
? 为什么
dataType: 'jsonp'
? If you don't intend to utilize jsonp, don't instruct jQuery to do this. 如果您不打算使用jsonp,请不要指示jQuery执行此操作。 See the docs: https://api.jquery.com/jQuery.ajax/
请参阅文档: https : //api.jquery.com/jQuery.ajax/
"jsonp": Loads in a JSON block using JSONP.
“ jsonp”:使用JSONP加载JSON块。 Adds an extra "?callback=?"
添加一个额外的“?callback =?” to the end of your URL to specify the callback.
URL的末尾以指定回调。 Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.
通过将查询字符串参数“ _ = [TIMESTAMP]”附加到URL来禁用缓存,除非将cache选项设置为true。
'obj='+JSON.stringify(ClientObject),
this will guarantee invalid json. 'obj='+JSON.stringify(ClientObject),
这将确保无效的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. 作为参考,请看以下问题: jQuery ajax,如何使用jQuery发送JSON而不是QueryString 。
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 并在
app.php
使用
$input = json_decode(file_get_contents('php://input'));
to get the data. 获取数据。 Use it like:
像这样使用它:
var_dump($input->ClientData[0]->firstName); // => string(6) "Master"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.