2014-11-26 6 views
4

에 오류 "더 헤더 존재 '액세스 제어는 - - 원산지 허용하지'는"나는 HTML 페이지Cherrypy

<script> 
    function getContent(page) 
    { 
     var xmlhttp; 
     if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp=new XMLHttpRequest(); 
      } 
     else 
      {// code for IE6, IE5 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       var json = xmlhttp.responseText; 
       obj = JSON.parse(json); 
       document.getElementById("content").innerHTML=obj.content; 
       document.getElementById("title").innerHTML=obj.title; 
      } 
     } 
    xmlhttp.open("GET","http://differentserver.com:8080?page="+page,true); 
    xmlhttp.send(); 
} 
</script> 

에서 다음과 같은 자바 스크립트와 JSON의 코드를 제공 cherrypy를 사용하여 파이썬 스크립트를 가지고 인 : 그러나

import cherrypy 
import json 

class ContentGeneratorService(object): 
exposed = True 
@cherrypy.tools.accept(media='text/plain') 
def GET(self, page='home'): 
    file_title = open(page + '.title', 'r') 
    file_content = open(page + '.content', 'r') 
    return json.dumps({"title": file_title.read().replace('\n', ''), "content":  file_content.read().replace('\n', '') }) 


def CORS(): 
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*" 

if __name__ == '__main__': 
conf = { 
    '/': { 
     'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 
     'tools.sessions.on': True, 
     'tools.response_headers.on': True, 
     'tools.response_headers.headers': [('Content-Type', 'text/plain')], 
     } 
    } 

cherrypy.server.socket_host = '0.0.0.0' 
cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS) 
cherrypy.config.update({'server.socket_port': 8080}) 
cherrypy.quickstart(ContentGeneratorService(), '/', conf) 

, 나는 "어떤 헤더 존재 '액세스 제어는 - - 원산지 허용하지'는"오류가 발생합니다. cherrypy로 CORS를 사용하는 방법이 있습니까?

감사합니다.

답변

5

이제는 작동하는 것 같습니다. 내가 추가했습니다

'tools.CORS.on': True 

conf.

+0

어디에 추가 했습니까? 구성 파일의 섹션 global 또는/ – VaidAbhishek

+0

현재 CherryPy 버전 ('10.2.1')에서는 더 이상 작동하지 않습니다. – Zelphir

+0

예, CherryPy 10 이상에서는이 설정도 필요합니다 :' 'tools.response_headers.on': True'. –

1

cherrypy.response.headers [ "액세스 제어 - 허용 - 기원"] = "*"이제 모든 사이트 서버에 AJAX 호출을 얻을 수 있기 때문에

은 매우 위험하다 파이썬 스크립트에 의해 제공되는 컨텐츠. 대신 [ "액세스 제어 - 허용 - 기원"]

cherrypy.response.headers을 사용 = "사이트 도메인"

훨씬 안전한 옵션입니다.