2016-07-01 5 views
2

xml 파일이있는 url에 http GET 요청을하고 있는데 응답 헤더 'Content-Lenght'을 가져와야합니까? 가져올 방법이 있습니까? 파일을 다운로드하기 전에 파일의 크기를 확인해야합니다. 로 형질 전환 응답 본문 - | {오브젝트 문자열} -angularjs - 콘텐츠 길이 헤더를 얻을 수있는 방법

여기 각도 문서에서 내 코드

$http({ 
    method : "GET", 
    url : "http://url/file.xml" 
    }).then(function mySucces(data) { 

    console.log(response); 
    $scope.content = response.data; 

    }, function myError(error) { 

    console.log(error); 
    $scope.content = error.statusText; 
    }); 

답변

1

의, https://docs.angularjs.org/api/ng/service/ $ HTTP

응답 콜백

  • 데이터를 가져 변환 함수.

  • 상태 - {number} - 응답의 HTTP 상태 코드입니다.

  • 헤더 - {function ([headerName])} - 헤더 게터 기능.

  • config - {Object} - 요청을 생성하는 데 사용 된 구성 객체입니다.

  • statusText - {문자열} - 응답의 HTTP 상태 텍스트입니다.

당신이 경우, 헤더 이름 headers 함수를 호출하여 응답 헤더를 얻을 수 headers('Content-Type')

$http({ 
    method: "GET", 
    url: "http://b31fe90a.ngrok.io/xml/XML-Meli.xml" 
}).then(function mySucces(response) { 

    console.log(response); 
    console.log(response.headers('content-type'); // it can be `Content-Type` not sure. but can be any header key. 
    $scope.content = response.data; 

}, function myError(error) { 

    console.log(error); 
    $scope.content = error.statusText; 
}); 
+0

이 헤더는 이것은 나를 위해 작동하지 않는 기능 –

+0

아니라고 말한다 'Content-Length'헤더. 귀하의 예제는 'Content-Type'을 보여 주지만 'Content-Length'를 요청하면 'null'만 반환됩니다. 나는 여전히 이것을 'Content-Length'로 작동시키는 방법을 찾지 못했다. – jkjustjoshing

+0

@jkjustjoshing 시도해 보셨습니까? http://stackoverflow.com/questions/38190998/angularjs-get-headers-in-response –

관련 문제