Axios, POST request to Flask

前端 未结 2 1737
刺人心
刺人心 2021-01-06 17:49

I try to make a POST to a flask server using axios:

var config = { headers: {  
                      \'Content-Type\': \'application/json\',
                        


        
相关标签:
2条回答
  • 2021-01-06 18:35

    You need to add CORS support to your Flask app. See a related threat here: Flask-CORS not working for POST, but working for GET. A popular CORS extension for Flask can be found here: https://flask-cors.readthedocs.io/en/latest/.

    0 讨论(0)
  • 2021-01-06 18:50

    If anyone else is stuck, be sure to check your before and after request methods. My problem was this:

    @app.before_request
    def oauth_verify(*args, **kwargs):
        """Ensure the oauth authorization header is set"""
        if not _is_oauth_valid():
            return some_custome_error_response("you need oauth!")
    

    So then this would raise an exception on any request, including an OPTIONS method. Of course, the fix is easy:

    @app.before_request
    def oauth_verify(*args, **kwargs):
        """Ensure the oauth authorization header is set"""
        if request.method in ['OPTIONS', ]:
            return
        if not _is_oauth_valid():
            return some_custome_error_response("you need oauth!")
    
    0 讨论(0)
提交回复
热议问题