2014-11-15 3 views
2

Angular JS를 배우고 있고 HTTP 요청 수신에 문제가 있습니다. 어떤 이유로 (내가 불을 지르고에서 디버깅 할 때 나는 응답이 ""것을 볼 거기에 아무것도 경고.) 나는 빈 오류 응답을 얻을각도 JS - 빈 오류 응답

function countryController($scope,$http) { 
delete $http.defaults.headers.common['X-Requested-With']; 
$http.get("http://localhost:8080/countrybook/api/v1/countrybook") 
    .success(function(response) { 
     $scope.countries = response; 
    }) 
    .error(function(response){ 
     alert(response); 
    }); 
} 

: 여기 내 코드입니다.

내 헤더는 다음과 같이 :

응답 헤더 :

Cache-Control private, must-revalidate 
Content-Length 355 
Content-Type application/javascript 
Date Sat, 15 Nov 2014 16:53:52 GMT 
Last-Modified Sat, 15 Nov 2014 16:52:06 GMT 
Server WebStorm 9.0.1 

요청 헤더 :

function countryController($scope,$http) { 
    delete $http.defaults.headers.common['X-Requested-With']; 
    $http.get("http://localhost:8080/countrybook/api/v1/countrybook") 
     .success(function(response) { 
      $scope.countries = response; 
     }) 
     .error(function(response){ 
      alert(response); 
     }); 
} 
:

Accept */* 
Accept-Encoding gzip, deflate 
Accept-Language en-US,en;q=0.5 
Cache-Control max-age=0 
Connection keep-alive 
Host localhost:63342 
If-Modified-Since Sat, 15 Nov 2014 16:52:06 GMT 
Referer http://localhost:63342/CountryBook/index.html 
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0 

내가 방화범 볼 수 있다는 응답은 이것이다

Coul 뭐가 잘못 됐는지 알아내는 걸 도와 주겠니? 브라우저에서 직접 http 요청을 받으면서 위에서 언급 한 동일한 링크를 사용하면 예상 한 응답을 볼 수 있습니다. 당신의 도움을 주셔서 감사합니다! 내 일반 $에 http 패턴 백엔드을 모르고

+0

Firefox 응답이 섞여있는 것 같습니다. 응답이 ""인 경우 알림이 올바르게 표시되는 것 같습니다. 귀하의 질문에 대한 답변이 백엔드 측면에서 발견 될 것이라고 가정합니다. – smartbart24

+0

안녕하세요 SmartBart24. 백엔드 로그를 확인했습니다. 내 백엔드가 정확한 응답을 발송합니다. 또한 브라우저에서 직접 사용하면 동일한 링크가 제대로 작동합니다. 방화 광에서 볼 수있는 응답은 위에서 언급 한 것과 같은 각도의 JS 코드입니다. 그리고 나는 경고를 공란으로 얻는다. 나는 방화범을 끌고 응답에 시계를 추가했다. 응답이 비어 있습니다. 내가 어디로 잘못 가고 있는지 확실하지 않습니다. – takeradi

+0

$ httpProvider.defaults.headers.common [ 'Accept'] = 'application/json, text/javascript'; $ httpProvider.defaults.headers.common [ 'Content-Type'] = 'application/json; charset = utf-8 ';'귀하의 코드 –

답변

3

문제가 있었다 : 또한 줄을 취하려고

The Same Origin Policy disallows reading the remote resource at localhost:8080/countrybook/api/v1/countrybook. This can be fixed by moving the resource to the same domain or enabling CORS. 

서버 측 응답에서이 두 헤더를 추가하여 해결 :

.header("Access-Control-Allow-Origin", "*") 
.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT") 

Smartbart24 -보고 콘솔 로그에서 도움이되었습니다. 당신의 도움을 주셔서 감사합니다!

1

그래서는 다음과 같습니다

$http.get(requestEndpointString) 
.success(function(data,status,headers,config){ 
    if(status!=200){ 
     /*unexpected response status ...*/ 
     console.log('API error status: '+status); 
    }else{ 
     /* Success ... */ 
    } 
}) 
.error(function(data, status, headers, config){ 
    /* Handle errors */ 
}); 

어쩌면 당신이 응답 상태를 로깅과 디버깅하는 데 도움이?

delete $http.defaults.headers.common['X-Requested-With']; 
+0

그게 너무 도움이됩니다. 나는 오류의 콜백이 단 하나의 인수 (응답)를 가지고 있다고 생각하고 있었고 response.status로 관리 할 수는 있지만 작동하지 않는다. 응답이 실제 데이터에 있었기 때문에 그 경우 데이터가 비어있었습니다. – C0ZEN