2012-09-22 2 views
0

게시 된 양식 필드에서 JSON 요청을 생성 한 다음 PHP 코드가 있습니다.jQuery ajax를 사용한 PHP JSON 결과

<?php 
if (isset($_POST['data']['Affiliate'])) { //Where ['data']['Affiliate'] is an array of posted field names of the form. 
    $data = $_POST['data']; 

    $base = 'https://api.whatever.com/Api?'; 

    $params = array(
     'Format' => 'json' 
     ,'account' => $data['Affiliate'] 
    ); 

    $url = $base . http_build_query($params); 
    $result = file_get_contents($url); 
    $result = json_decode($result); 

    echo "<pre>"; 
    print_r($result); 
    echo "</pre>"; 
?> 

print_r($result)은 다음을 인쇄합니다.

stdClass Object 
(
    [request] => stdClass Object 
     (
      [Format] => json 
      [account] => stdClass Object 
       (
        [country] => US 
        [address1] => asdasd 
        [city] => asdasd 
        [zipcode] => asdasd 
        [phone] => asdasd 
       ) 
     ) 

    [response] => stdClass Object 
     (
      [status] => -1 
      [data] => 
      [errors] => Array 
       (
        [0] => stdClass Object 
         (
          [err_code] => 3 
          [err_msg] => A user already exists with this email address. 
          [attribute_name] => Email 
          [publicMessage] => User account or user is not valid. 
         ) 
        [1] => stdClass Object 
         (
          [err_code] => 1 
          [err_msg] => City cannot be blank. 
          [attribute_name] => city 
         ) 
       ) 
     ) 
) 

어떻게 동일한 결과를 달성하고 <li> 태그 오류의 배열을 인쇄 할 JQuery와 아약스를 사용할 수 있습니다.

+1

다른 요청을하기 위해 ['jQuery.ajax'] (http://api.jquery.com/jQuery.ajax/)와 같은 방법으로? 프로세스의 어떤 부분에서 문제가 발생합니까? – DCoder

+0

@Dcoder, JSON으로 ajax 유형을 사용합니까? 양식을 serialize합니까? 어떻게하면 오류 배열을 정렬 할 수 있습니다. 이 부분에 대한 몇 가지 jquery 도움말이 필요합니다. – hedhud

답변

0

여기에서했던 것처럼 JSON을 PHP 객체로 디코딩하지 말고 순수한 JSON을 유지하고 AJAX 요청에 제공 한 다음 디코딩 클라이언트 쪽 (나는 거기에 당신을 위해 이미 그것을 얻고 디코딩 특별한 jquery 아약스 함수가 ​​있다고 믿는다. 그것에 대한 라비의 대답을 참조하십시오)

하지만 당신이 이것을 원하지 않는다면 : 왜 접근하지 않습니까? 너는 json_decode()에서 얻은 물건의 들판?
나는 $result->$response->$errors이 찾고있는 오류의 배열이라고 생각합니다.

+0

나는이 길로 갔다. 감사. – hedhud

3
var errorsArr=''; 
var affiliate=''; 
//affiliate is your data of $data['Affiliate'] which you can get using jquery or js.. 
$.ajax({ 
    url:'https://api.whatever.com/Api?' 
    data:{Format: 'json',account : affiliate}, 
    dataType:'json', 
    success:function(data){ 
     errorsArr=data.response.errors; 
    } 
    } 
}); 

오류가있는 모든 오류가 발생합니다 .Arr. 원하는 출력을 위해 iterate 할 수있는 json 배열입니다.

+0

'data : {Format : 'json', account => affiliate,'모든 양식 필드가 어떻게 문자열 화됩니까? 데이터에서'account => affiliate '는 어떻게 작동합니까? – hedhud

+0

@hedhud 서버로 다시 보내기 전에 json_encode ($ result)를 사용해야합니다. 그것이 JSON을 문자열로 변환하는 방법입니다. – Ohgodwhy

+0

변수 (계열사)에서 양식을 serialize 할 수 있습니다. – Ravi