JSON PHP解码不起作用 [英] JSON PHP decode not working
问题描述
我看过很多例子,但是无论出于什么原因,似乎没有一个适合我.
I have seen many examples, but for whatever reason, none seem to work for me.
我将以下内容通过ajax从应用程序发送到php文件.发送时的外观如下:
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",
}]
}
原样代码:
{"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"}]}
这在一个长行中进入了一个php文件,这是代码:
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.
谢谢:)
更新
以下是创建对象的代码:
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')
}
]
};
这是它的发送方式:
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) {
}
});
}
推荐答案
有些事情会引起问题:
-
为什么
dataType: 'jsonp'
?如果您不打算使用jsonp,请不要指示jQuery执行此操作.请参阅文档: https://api.jquery.com/jQuery.ajax/
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":使用JSONP加载JSON块.增加了一个额外的 ?callback =?" URL的末尾以指定回调.禁用 通过将查询字符串参数"_ = [TIMESTAMP]"附加到 URL,除非将cache选项设置为true.
"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.
'obj='+JSON.stringify(ClientObject),
这将确保无效的json.
'obj='+JSON.stringify(ClientObject),
this will guarantee invalid json.
作为参考,请看以下问题: jQuery ajax,如何发送JSON而不是QueryString 用jQuery发送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) {
}
});
}
在app.php
中使用
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"
这篇关于JSON PHP解码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!