[英]How do you do post request to Flask backend api using vue js?
I am trying to do simple signup process using vue js and flask back end. 我正在尝试使用vue js和flask后端进行简单的注册过程。 But I get this error message.
但我收到此错误消息。
XMLHttpRequest cannot load http://localhost:5000/users . XMLHttpRequest无法加载http:// localhost:5000 / users 。 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:8080 ' is therefore not allowed access.
因此,不允许访问源' http:// localhost:8080 '。
Why is this happening? 为什么会这样呢?
My vue js code is following 我的vue js代码如下
methods: {
signup() {
this.$http.post("http://localhost:5000/users", this.user)
.then(function(data) {
alert(data)
})
}
}
my flask back end code is following 我的烧瓶后端代码如下
@app.route('/users', methods=['POST'])
def users():
username = request.form.get('username')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
first_name = request.form.get('first_name')
last_name = request.form.get('last_name')
email = request.form.get('email')
file = request.files['avatar']
if file.filename:
if allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'],
filename))
else:
error = "Your avatar has be to an image file"
redirect(url_for('signup'))
else:
filename = ""
if password1 == password2:
user = User(username = username, password = password1, first_name = first_name, last_name = last_name, email = email)
user.save_to_db()
If you are hosting both of them locally you could try running web browser eg Chrom with out web security Use the following code in CLI for windows. 如果您都在本地托管它们,则可以尝试运行网络浏览器,例如Chrom而没有网络安全性。在Windows的CLI中使用以下代码。 Hope it will help!
希望对您有所帮助!
chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
You are serving your app over http://localhost:8080/ while your API is at http://localhost:5000 and these two are different origins for the browser! 您的应用程序通过http:// localhost:8080 /提供服务,而API的地址为http:// localhost:5000,而这两个是浏览器的不同来源!
A resource makes a cross-origin HTTP request when it requests a resource from a different domain, protocol, or port to its own.
资源从其他域,协议或端口向其自己的资源请求资源时,会发出跨域HTTP请求。 For example, an HTML page served from http://domain-a.com makes an src request for http://domain-b.com/image.jpg
例如,从http://domain-a.com提供的HTML页面发出了对http://domain-b.com/image.jpg的src请求。
For security reasons , browsers restrict cross-origin HTTP requests initiated from within scripts.
出于安全原因 ,浏览器限制了从脚本内部发起的跨域HTTP请求。 For example, XMLHttpRequest and Fetch follow the same-origin policy.
例如,XMLHttpRequest和Fetch遵循同源策略。 So, a web application using XMLHttpRequest or Fetch could only make HTTP requests to its own domain.
因此,使用XMLHttpRequest或Fetch的Web应用程序只能向其自己的域发出HTTP请求。
The Cross-Origin Resource Sharing (CORS) mechanism gives web servers cross-domain access controls, which enable secure cross-domain data transfers.
跨域资源共享(CORS)机制为Web服务器提供了跨域访问控制,可实现安全的跨域数据传输。 Modern browsers use CORS in an API container - such as XMLHttpRequest or Fetch - to mitigate risks of cross-origin HTTP requests.
现代浏览器在API容器(例如XMLHttpRequest或Fetch)中使用CORS来减轻跨源HTTP请求的风险。
In your case either use flask-cors
or host your API on the same domain as your app. 在您的情况下,请使用
flask-cors
或将您的API托管在与您的应用相同的域中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.