많은 사례를 보았습니다. 그러나 어떤 이유로 든 아무 것도 저에게 효과가없는 것 같습니다.JSON PHP 디코드가 작동하지 않습니다.
다음과 같이 ajax를 통해 PHP 파일로 전송됩니다.
obj:{"ClientData": [{ "firstName":"Master", "lastName":"Tester", "email":"[email protected]", "dob":"1973-01-22", "age":"51", }], "HealthData": [ "condition : Prone to Fainting/Dizziness", "condition : Allergic Response to Plasters", ], "someData": [{ "firstName":"Male", "lastName":"checking", }] }
코드와 같이 : 이것은 그하여 전송이 때 모습입니다 이것은 PHP 파일에 하나의 긴 줄에 제공
{"ClientData":[{"firstName":"Master","lastName":"Tester","email":"[email protected]","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
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']);
?>
아무리 노력해도 정보를 볼 수 없으며 오류가 발생하지 않습니다. 그냥 비어 있습니다! 누군가 제게 올바른 방향으로 나를 가르쳐 줄 수 있습니까?
감사합니다 :)UPDATE 여기
객체 생성 코드입니다 :
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) {
}
});
}
이것은 저에게 유효한 json처럼 보이지 않습니다. –
var_dump ($ Ojb)하면 어떻게됩니까? null을 얻으면 json은 유효하지 않습니다. 여전히 빈 페이지가 나타나면 PHP 오류가 발생합니다. json 모듈을 서버에 설치하지 않았을 수 있습니다. – quano
가능한 복제 http://stackoverflow.com/questions/16828647/php-json-decode-does-not-work –