我正在尝试从Angular 5应用程序发送数据到cherrypy后端。我能够调用正确的cherrypy函数并获得200响应,但我的参数都无法通过。
吸引我眼球的一件事是,当我在chrome调试器中单击view source时,我的有效负载看起来像这个{"username":"admin","password":"admin"}。这是不是应该有不同的格式?
这是我的帖子:
getUserDetails(username, password) {
const _headers = new HttpHeaders();
const headers = _headers.append('Content-Type', 'application/json');
const options = {headers: headers };
this.data = JSON.stringify({ username, password });
return this.http.post(this.url1 + 'login', this.data, options )
.subscribe(data => {
console.log(data);
});
}同样,这会到达正确的端点,只是没有数据通过。
下面是cherryPy:
登录功能:
class authServer(object):
@cherrypy.expose
def login(self,*args, **kwargs):
print(type(args),args, kwargs)我已经尝试了各种参数,如果我有username和password,我得到的错误是缺少参数。
下面是cherrypy配置。
def CORS():
"""Allow AngularJS apps not on the same server to use our API
"""
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers["Access-Control-Allow-Headers"] = \
"content-type, Authorization, X-Requested-With"
cherrypy.response.headers["Access-Control-Allow-Methods"] = 'GET, POST'
if __name__ == '__main__':
cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
cherrypy.log.error_log.propagate = False
cherrypy.log.access_log.propagate = False
cherrypy.config.update({'server.thread_pool': 30,
'tools.CORS.on': True,
'server.socket_host': '0.0.0.0',
'server.socket_port': 8081})
cherrypy.quickstart(authServer(), '/')我以前在cherrypy上发过无数的帖子。这里唯一的区别是我使用的前端。任何帮助都将不胜感激。
谢谢,
发布于 2018-08-01 19:44:18
原来是CORS的问题。我更改了CORS函数,如下所示:
def CORS():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers["Access-Control-Allow-Headers"] = \
"content-type, Authorization, X-Requested-With"
cherrypy.response.headers["Access-Control-Allow-Methods"] = 'GET, POST'要这样做:
def CORS():
if cherrypy.request.method == 'OPTIONS':
cherrypy.response.headers['Access-Control-Allow-Methods'] = 'POST'
cherrypy.response.headers['Access-Control-Allow-Headers'] = 'content-type'
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
return True
else:
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
cherrypy.tools.CORS = cherrypy._cptools.HandlerTool(CORS)在我的main函数上面,我放了这个:
@cherrypy.expose
@cherrypy.config(**{'tools.CORS.on': True})
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()https://stackoverflow.com/questions/51517033
复制相似问题