我正在尝试将Angular 9应用程序中的数据提交到Bottle后端服务器。在客户端,我得到了CORS错误,“ Access-Control-Allow-Origin”标头丢失了。它还说,同源规则阻止了该请求。在后端,它仅显示状态代码405。但是,当我通过邮递员执行请求时,一切正常。为了使其正常工作,我必须进行哪些更改?我希望它也可以在生产模式下工作。这是我的代码:Angular component.ts:
onSubmit(form) {
console.log(form.value);
this.http.post('http://localhost:8080/contact', form.value).subscribe();
}
我的瓶子后端服务器:
@post('/contact')
def progress_contact():
response.set_header('Content-Type', 'application/json')
response.set_header('Access-Control-Allow-Origin', 'http://localhost:4200')
response.set_header('Access-Control-Allow-Methods', 'PUT, GET, POST, OPTIONS, DELETE')
response.set_header('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin, Content-Type, Accept, Accept-Language, Origin, User-Agent')
print(request)
print(request.POST.moin)
print(request.POST.email)
return 'hello'
您需要在后端代码中允许使用cors。您有问题,因为有待检查的角度请求,并且后端代码不允许使用cors。有多个选项可以允许您使用cors,在这里您也可以使用pypi上的软件包。https://pypi.org/project/bottle-cors/