目前,引入了 JSON 來處理請求表單。我已經不止一次地編寫了課程中解釋的代碼,并且我有 99% 的信心我確實正確地編寫了所有內容。但當我嘗試使用請求表單時,我總是收到錯誤“405 method not allowed”。網上搜索沒有得到任何結果,我找不到解決我的問題的方法。下面是它應該運行的 python 路線:[email protected]('/todos/create', methods=['POST'])?def create_todo():? ? description = request.get_json()['description']? ? todo = Todo(description=description)? ? db.session.add(todo)? ? db.session.commit()? ? return jsonify({? ? ? ? 'description': todo.description? ? })這里是 HTML 腳本?<script>? ? ? ? ? ? document.getElementById('form').onsubmit = function(e){? ? ? ? ? ? ? ? e.preventDefault();?? ? ? ? ? ? ? ? fetch('/todos/create', {? ? ? ? ? ? ? ? ? ? method: 'POST',? ? ? ? ? ? ? ? ? ? body: JSON.stringify({? ? ? ? ? ? ? ? ? ? ? ? 'description': document.getElementById('description').value? ? ? ? ? ? ? ? ? ? }),? ? ? ? ? ? ? ? ? ? headers: {? ? ? ? ? ? ? ? ? ? ? ? 'Content-Type': 'application/json'? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? })?? ? ? ? ? ? ? ? .then(function(response){? ? ? ? ? ? ? ? ? ? return response.json();?? ? ? ? ? ? ? ? })? ? ? ? ? ? ? ? .then(function(jsonResponse){? ? ? ? ? ? ? ? ? ? console.log(jsonResponse);? ? ? ? ? ? ? ? ? ? const lil_item = document.createElement('LI');?? ? ? ? ? ? ? ? ? ? lil_item.innerHTML = jsonResponse['description'];? ? ? ? ? ? ? ? ? ? document.getElementById('todos').appendChild(lil_item);?? ? ? ? ? ? ? ? });?? ? ? ? ? ? }? ? ? ? </script>我希望有人能幫助我,我真的不知道如何解決這個問題。提前致謝。
405 不允許方法 - JSON - 請求的 URL 不允許使用該方法
慕少森
2023-07-29 16:04:43