Meteor

2014-10-30 4 views
0

에서 클라이언트 측의 "Content-Type"헤더 설정 Meteor (v1.0) HTTP.call 메서드를 사용하여 헤더에 application/json 콘텐트 유형 만 허용하는 Python 기반 서버와 통신하려고합니다. 하지만 Meteor에서 클라이언트 측에서 API URL을 호출 할 때 HTTP 헤더를 올바르게 설정할 수는 없습니다. 나는이 같은 유성의 서버 측에서 같은 URL을 호출하면, 그러나Meteor

if (Meteor.isClient) { 
    Template.testing.events({ 
    'click button': function(event, tpl) { 
     event.preventDefault(); 

     var method = 'GET'; 
     var url = 'http://localhost:6543/test'; 
     var options = { 
     headers: {'Content-Type': 'application/json'} 
     } 

     HTTP.call(method, url, options, function(error, result) { 
     if (error) { 
      console.log('ERRR'); 
      console.log(error); 
     } else 
      console.log('RESULT'); 
      console.log(result); 
     }); 
    } 

    }); 
} 

:이 같은 조각으로

는 파이썬 서버에서 415 (Unsupported Media Type) 오류가

if (Meteor.isClient) { 
    Template.testing.events({ 
    'click button': function(event, tpl) { 
     event.preventDefault(); 

     var method = 'GET'; 
     var url = 'http://localhost:6543/test'; 
     var options = { 
     headers: {'Content-Type': 'application/json'} 
     } 

     Meteor.call('APICall', method, url, options, function (error, result) { 
     if (error) { 
      console.log('CLIENT ERRR'); 
      console.log(error); 
     } else { 
      console.log('CLIENT RESULT'); 
      console.log(result); 
     } 
     }); 
    } 

    }); 
} 

if (Meteor.isServer) { 
    Meteor.methods({ 
    APICall: function (method, url, options) { 
     HTTP.call(method, url, options, function(error, result) { 
     if (error) { 
      console.log('SERVER ERRR'); 
      console.log(error); 
     } else 
      console.log('SERVER RESULT'); 
      console.log(result); 
     }); 
    } 
    }); 
} 

나는 서버로부터 적절한 응답을 얻는다.

파이썬 측에서 가능한 모든 요청에 ​​대해 CORS 출처를 활성화했습니다 (예 : cors_origins=('*')).

그래서 ... 클라이언트 측에서 헤더를 설정할 수 있습니까? 아니면 항상 서버 측에서이 서비스를 호출해야합니까?

답변

0

저는 클라이언트 쪽에서 어떤 성공도하지 못했습니다. 유성 HTTP 패키지의 HTTP.call 클라이언트 부분을 체크 아웃 :

https://github.com/meteor/meteor/blob/devel/packages/http/httpcall_client.js

는 대부분이 비 호환성 및 물건처럼, 문제의 호스트로 이어질 수있는 클라이언트 측에 브라우저 XHR 객체를 사용합니다. 당신은 (line 136 주위) 코드 주석 중 하나에서 참조 문제를 볼 수 있습니다

And when you check out the server implementation 당신이 내 책에, 매우 신뢰할 수있는, (connect에서)을 request 라이브러리를 사용하여 확인하실 수 있으며, 균일 한 결과를 생성 할 수 있습니다 모든 사용자에게 제공됩니다 (브라우저 차이로 인해 춤추는 것은 아닙니다).

내 선택과 추천은 분명히 서버 측 호출이 될 것입니다. 제대로 작동하고 안정적 ​​일뿐만 아니라 시스템의 내부 동작을 클라이언트/최종 사용자에게 알릴 필요가 없으므로 사용자의 입장에서 '더 안전합니다'. 누가 알아? 파이썬 기반 서버에서 실행되는 API에 민감한 데이터가있을 수 있습니다.

+0

답변 해 주셔서 감사합니다. 궁금해서 "책"에 대해 언급하셨습니까? 어느 쪽 이냐? – errata

+1

아! [이디엄입니다] (http://idioms.thefreedictionary.com/in+my+book). 내가 실제로 책을 썼 더라면 좋겠지 만 :) –

+0

아 롤, 나는 본다 : 결코 그 하나를 지금까지 듣지 않았다 hahahaha :) – errata

관련 문제