2014-02-24 6 views
0

저는 AngularJS로 시작했으며 매우 기본적인 예제를 시도했습니다. 웹 페이지에는 버튼이 있으며,이 버튼을 클릭하면 서버에서 json을 요청할 수 있습니다. 하지만 버튼을 클릭 할 때마다 코드가 오류 기능의 일부를 실행합니다.AngularJS로 JSON을 구문 분석 할 수 없습니다.

HTML 파일

<!DOCTYPE html> 
<head> 
    <title> step 4</title> 
</head> 

<body ng-app="step4App"> 
    <div ng-controller="FriendsCtrl"> 

     <button ng-click="loadFriends()">Load Friends</button> 
    </div> 

    <script src="angular.min.js"></script> 
    <script src="xml2json.js"></script> 
    <script src = "step4.js"></script> 
</body> 
</html> 

JS

var app = angular.module("step4App",[]); 

app.controller("FriendsCtrl", function($scope,$http) { 
    $scope.loadFriends = function(){ 



     $http({method:'GET',url:'http://localhost:8080/test.jsp', params:{name:"abc",no:"LBF1151638"}}).success(function(data) { 
      $scope.friends = data; 
      document.write('<p>two</p>'); 
      document.write(data); 
     }).error(function() { 

      alert("error"); 
      }); 
      } 

}); 

지금 JSON 응답을 반환 현재 JSP 페이지.

{"response":{"numFound":0,"start":0,"docs":[]}} 

하지만 왜 항상 오류 기능을 실행하는지 이해할 수 없습니다.

브라우저 콘솔에서 요청이 작성되었으며 47 바이트 응답을 받았습니다 (JSON의 문자 수와 같음). 하지만 JSON이 인쇄되지는 않습니다.

답변

1

서버에 문제가있을 수 있습니다. FireFox + Firebug를 사용하여 페이지를 열어 서버 응답을 검사하여 예상 한 내용을 확인하십시오 (상태 코드 및 Content-Type 응답 헤더 확인).

명령 줄에서 curl -v http://localhost:8080/test.jsp을 실행할 수도 있습니다.

BTW Angular는 콜백에서 추가 매개 변수를 전달합니다. 그들에게 경고하는 것을 시도해보십시오. http://docs.angularjs.org/api/ng/service/$http:

$http({method: 'GET', url: '/someUrl'}). 
success(function(data, status, headers, config) { 
    // this callback will be called asynchronously 
    // when the response is available 
}). 
error(function(data, status, headers, config) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
}); 
+0

감사합니다. 나는 방화범을 사용하여 노력했다. 상태는 200 (OK)이지만 angularJS는 404라고 말합니다. – krammer

+0

JSP 파일의 논리를 확인하고 싶을 때가 있습니다. 또는 URL에 오타가 없는지 확인하십시오. –

관련 문제