如何从JSON响应重定向? [英] How to redirect from a JSON response?
问题描述
因此,我尝试使用Flask和Javascript上传器(Dropzone)上传文件,并在上传完成后重定向.文件可以正常上传,但可以使用flask中的传统重定向:
So I am trying to use Flask and a Javascript uploader(Dropzone) to upload files and redirect after the upload is complete. Files are getting uploaded fine, but using the traditional redirect in flask:
return (redirect ("http://somesite.com"))
不执行任何操作,页面不会更改.我认为这是因为:将要发送的文件的请求标头设置为 Accept:"application/json"
,并且响应标头以<"text/html; charset = utf-8"
如何返回json响应,然后从中重定向?只是
Does nothing, page does not change. I think this is because of this: The request headers of the files being sent are set to Accept:"application/json"
, and the response headers are being sent in <"text/html; charset=utf-8"
How can I return a json response and then redirect from it? Just doing
return (redirect (jsonify("http://somesite.com")))
给出错误:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
我知道浏览器无论如何都不会从Json标头重定向.如何发送从Flask重定向到我的JS应用客户端的URL,并从那里重定向?我已经使用普通的HTML表单对其进行了测试,以提交文件,并且效果很好,因此,我很确定这是JSON问题.谢谢你.
I know browsers won't redirect from Json headers anyway. How can I send the url to redirect to from Flask back to my JS app clientside and redirect from there? I already tested this with a normal HTML form to submit the files and it worked perfectly so I'm pretty sure it is the JSON issue. Thank you.
推荐答案
您可以为响应设置模仿类型:
You can set the mimetype for the response:
response = redirect('http://somesite.com')
response.mimetype = 'application/json'
return response
或者:
return make_response(redirect('http://example.com'), mimetype='application/json')
这篇关于如何从JSON响应重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!