Http 角形5至樱桃色柱
我正在尝试从Angular 5应用程序向cherrypy后端发送数据。我能够调用正确的cherrypy函数并得到200的响应,但是我的参数都没有通过 我注意到的一件事是,当我在chrome调试器中单击view source时,我的负载看起来像这样{username:admin,password:admin}。这应该是不同的格式吗 这是我的帖子:Http 角形5至樱桃色柱,http,post,angular5,cherrypy,Http,Post,Angular5,Cherrypy,我正在尝试从Angular 5应用程序向cherrypy后端发送数据。我能够调用正确的cherrypy函数并得到200的响应,但是我的参数都没有通过 我注意到的一件事是,当我在chrome调试器中单击view source时,我的负载看起来像这样{username:admin,password:admin}。这应该是不同的格式吗 这是我的帖子: getUserDetails(username, password) { const _headers = new HttpHeaders(
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)
我尝试过各种参数,如果我有用户名和密码,我会得到一个错误,说缺少参数
这是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发过无数的帖子。这里唯一的区别是我使用的前端。任何帮助都将不胜感激
谢谢,原来是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)
在我的主要职能之上,我提出:
@cherrypy.expose
@cherrypy.config(**{'tools.CORS.on': True})
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
在工具中使用json_,cherrypy.request.json中将有一个dict