Cherrypy/Python webservice를 빌드하려고합니다. 나는 이미 크로스 도메인 아약스 요청을 가능케하는 방법을 찾는 데 하루 종일 쓰고 있습니다. 그게 마침내 효과가 있지만 이제 다음 문제가 생겼습니다. 이미 솔루션을 알고 있다고 생각하지만 구현 방법을 모르겠습니다.415 Cherrypy webservice
'tools.json_in.force': False
: 구성에이 줄을 추가하고,
415 Unsupported Media Type
Expected an entity of content type application/json, text/javascript
Traceback (most recent call last): File "/Library/Python/2.7/site-packages/cherrypy/_cprequest.py", line 663, in respond self.body.process() File "/Library/Python/2.7/site-packages/cherrypy/_cpreqbody.py", line 996, in process super(RequestBody, self).process() File "/Library/Python/2.7/site-packages/cherrypy/_cpreqbody.py", line 538, in process self.default_proc() File "/Library/Python/2.7/site-packages/cherrypy/_cperror.py", line 411, in __call__ raise selfHTTPError: (415, u'Expected an entity of content type application/json, text/javascript')
내가 찾은 솔루션 및 테스트하려고 : 문제는 내가 아약스 요청을 보낸다 때 Cherrypy 서버가 응답이다
import cherrypy
import json
import sys
class RelatedDocuments:
def index(self):
return "Hello World!"
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def findRelated(self, **raw):
#Get JSON message form request
request = cherrypy.request.json
result = []
#SOME CODE...
return result;
# Expose the index method through the web. CherryPy will never
# publish methods that don't have the exposed attribute set to True.
index.exposed = True
findRelated.exposed = True
def CORS():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
import os.path
tutconf = os.path.join(os.path.dirname(__file__), 'webserver.conf')
config = {
'global': {
'server.socket_host':'127.0.0.1',
'server.socket_port': 8080,
'log.error_file' : 'Web.log',
'log.access_file' : 'Access.log'
},
'/': {
'tools.CORS.on': True
}
}
if __name__ == '__main__':
cherrypy.tools.CORS = cherrypy.Tool('before_finalize', CORS)
cherrypy.quickstart(RelatedDocuments(),config=config)
내가 tools.CORS.on 라인 아래에있는 설정 라인을 추가하지만, 작동하지 않았다 :
그래서 나는이 코드를 구현하기 위해 노력했다. 다음으로이 시도 :
cherrypy.config.update({
'tools.json_in.force': False,
});
나는 findRelated 방법 위에서이 권리를 구현하기 위해 노력 eiter..next 작동하지 않았다 : 구현의 모든 나에게 500 오류, 정말 준
@cherrypy.config(**{'tools.json_in.force': False})
누군가가 나를 도울 수 있으면 고마워. 미리 감사드립니다!
정말 고마워, 너는 내 하루를 만들었 어! 모든 것이 이제는 정상적으로 작동하며 프리 플라이트 요청에 대해 들어 본 적이 없습니다. – Chris