2014-10-19 2 views
0

모바일 응용 프로그램에서 json 데이터를 ajax 호출을 통해 전달하려고합니다. 내 Ajax 호출은 postData를의 값은 내가 콘솔에서의 postData JSON을 볼 수ajax corova를 사용하여 서버로 보낸 JSON 데이터를 가져올 수없고 디코딩 할 수 없습니다.

{ event_name: "Contraptions" event_type: "Engineering" participant_type: "Individual" place: "Third" qr1: "amsds" qr2: "" qr3: "" qr4: "" qr5: "" round: "3" teamname: ""} 

입니다

$.ajax({ 
     type: 'GET', 
     data: postData, 
     jsonp: "callback", 
     dataType: 'jsonp', 
     url: 'http://*****.*****.org.in/qr.php', 
     success: function(data){ 
      $ionicLoading.hide(); 
      console.log(data); 
      alert('Your data was successfully added'); 
      $location.path('/home'); 
     }, 
     error: function(data,error){ 
      $ionicLoading.hide(); 
      console.log(error); 
      alert('There was an error adding your data'); 
     } 
    }); 

입니다.

내 서버 측 코드 qr.php는

<? 
try 
{ 
$callback = isset($_GET['callback']) ? preg_replace('/[^a-z0-9$_]/si', '', $_GET['callback']) : false; 
header('Content-Type: ' . ($callback ? 'application/javascript' : 'application/json') . ';charset=UTF-8'); 
$json = json_decode('postData'); 
$data = array('status' => $json); 
echo ($callback ? $callback . '(' : '') . json_encode($data) . ($callback ? ')' : ''); 

} 
catch(Exception $e) 
{ 
echo $e; 
} 
?> 

입니다하지만 디코딩은 $ JSON은 NULL입니다. JSON 데이터를 가져와 클라이언트 ?????에서 전달 된 JSON을 어떻게 디코딩해야합니까?

+0

postData는 JSON이 아니므로 [JSONLint] (http://jsonlint.com/)를 사용하여 데이터를 테스트하십시오. 데이터를 구문 분석 할 수 없으면 NULL이 리턴됩니다. – Barry

답변

0

JavaScript에서 올바른 개체 만들기

f.i.

var data = {event_name: "Contraptions", event_type: "Engineering", participant_type: "Individual"}; 

와 아약스 호출에

data : {postData: JSON.stringify(data)}; 

를 사용합니다.

+0

덕분에 @ 바리. 이것은 나를 위해 완벽하게 작동했습니다. :) – dharmesh

관련 문제