我正在用html中的用戶輸入一堆輸入,然後我將這些輸入傳遞給ajax查詢以獲取響應。Ajax GET請求不能與Flask一起使用
$.ajax({
url:"http://0.0.0.0:8080/getReport",
type:"GET",
data:JSON.stringify(out),
dataType:"json",
contentType:"application/json"
})
這是用於上述請求的Flask代碼。
@app.route('/getReport', methods=['GET'])
def report():
return Response('this is a sample response')
上述方法無法通過get找到'report'的路由。但是,它可以在POST請求中找到它。
這是我得到
127.0.0.1 - - [25/Apr/2016 13:00:03] "GET /getReport?{%22insertion_id%22:%22%22,%22start%22:%22%22,%22end%22:%22%22} HTTP/1.1" 400 -
壞請求日誌..我在做什麼錯在這裏?
您正在使用'JSON'作爲URL參數,爲什麼? – dm295
我不認爲Flask接受來自客戶端的json數據。你應該發送urlencoded數據,比如'data:{'key':'val',}'。不需要設置'dataType'和'contentType'選項。 – xyres
您應該序列化表單數據,而不是指定dataType或contentType。發送,它應該工作正常。 – tadamhicks