2017-07-28 98 views
-1

我嘗試使用Axios公司做出POST到燒瓶中,服務器:愛可信,POST請求燒瓶

var config = { headers: { 
         'Content-Type': 'application/json', 
         'Access-Control-Allow-Origin': '*'} 
      } 
axios.post("http://127.0.0.1:5000/test", 
      { label : "Test" , text : "Test"} , config 
     ) 
      .then(function (response) { 
      console.log(response); 
      }) 
      .catch(function (error) { 
      console.log(error); 
      }); 

現在燒瓶中的一部分

... 
data = request.get_json(silent=True) 
item = {'label': data.get('label'), 'text': data.get('text')} 
print item 
... 

不過,我會結束與以下錯誤:

XMLHttpRequest無法加載http://127.0.0.1:5000/test。對預檢請求的響應不會通過訪問控制檢查:請求的資源上不存在「訪問控制 - 允許來源」標頭。因此不允許訪問原產地'http://localhost:3000'。

爲什麼?我按照建議設置標題。

這裏的解決方案

from flask_cors import CORS, cross_origin app = Flask(__name__) cors = CORS(app, resources={r"/YOURAPP/*": {"origins": "*"}})

回答

0

如果其他人被卡住了,一定要檢查你的beforeafter請求方法。我的問題是這樣的:

@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!") 

所以那麼這將提高人們對異常任何要求,包括OPTIONS方法。當然,修復很簡單:

@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!")