2014-11-29 7 views
0

데이터를 JSON URL에서 가져 오려고합니다. 내 데이터가 JSON 배열 내에 배열되어 있습니다. 여기서는 꽤 새롭기 때문에 제게 데이터를 읽는 방법을 제안 해주세요. 고마워요. . 여기 null의 속성 'length'을 읽을 수 없습니다.

아약스 결과의 성격에 대해 아무 생각이없는 내 코드 .. 내가 오류가 JSON에서 값을 읽을의 시간에

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>Privacy Policy</title> 
</head> 
<body> 
<div id="showdata"></div> 
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
<script> 
$(function(){ 

var url = "http://portal.entitledirect.com/test/entitle_news.php"; 
var data = {"news_type": "docs","dt":"1417222458109"}; 
$.ajax({ 
    url: url, 
    data:data, 
    dataType: "jsonp", 
    contentType: 'application/json', 
    type: 'GET', 
    jsonpCallback: 'methodCaa', 

    success: function (result) { 
     console.log(result); 
     $.each($.parseJSON(result), function (item, value) { 
      if (item == "rates") { 
       $.each($.parseJSON(value), function (i, object) { 
        console.log(i + "=" + object); 
       }); 
      } 
     }); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
      console.log(jQuery.parseJSON(jqXHR)); 
      console.log(textStatus); console.log(errorThrown); 
    } 

}); 



}); 

function methodCaa(data){ 
    console.log((data)); 

} 





</script> 
</body> 
</html> 

...

Uncaught TypeError: Cannot read property 'length' of nulljquery-1.7.1.min.js:2 e.extend.eachjsonindex.html:25 $.ajax.successjquery-1.7.1.min.js:2 njquery-1.7.1.min.js:2 o.fireWithjquery-1.7.1.min.js:4 wjquery-1.7.1.min.js:4 d.onload.d.onreadystatechange 
+0

이의 구조를 게시하세요'result' ... –

+0

@krillgar jQuery를 내부적으로 사용 할 수 ... –

+0

는'$ .parseJSON을()'사용하지 마십시오; 결과는 이미 구문 분석됩니다. – JJJ

답변

0

입니다 그리고 당신이하려고하는 것은 여기에 있습니다. 그는 응답 처리에 대한 올바른 구문 :

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>Privacy Policy</title> 
</head> 
<body> 
<div id="showdata"></div> 
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
<script> 
$showdata = $('#showdata'); 
$(function(){ 
    var url = "http://portal.entitledirect.com/test/entitle_news.php"; 
    var data = {"news_type": "docs","dt":"1417222458109"}; 
    $.ajax({ 
     url: url, 
     data:data, 
     dataType: "jsonp", 
     contentType: 'application/json', 
     type: 'GET', 
     jsonpCallback: 'methodCaa', 

     success: function (result) { 
      console.log(result); 
      $.each(result, function (item, value) { 
       console.log("Processing index "+ item); 
       //if (item == "rates") { 
        $.each(value, function (i, object) { 
         $.each(object, function(j, objectValue){ 
          console.log(j, objectValue); 
          $showdata.append(j +": "+objectValue+"<br />"); 
         }); 
        }); 
       //} 
      }); 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
       console.log(jQuery.parseJSON(jqXHR)); 
       console.log(textStatus); console.log(errorThrown); 
     } 

    }); 
}); 
function methodCaa(data){ 
    console.log((data)); 

} 
</script> 
</body> 
</html> 
관련 문제