亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

405 不允許方法 - JSON - 請求的 URL 不允許使用該方法

405 不允許方法 - JSON - 請求的 URL 不允許使用該方法

慕少森 2023-07-29 16:04:43
目前,引入了 JSON 來處理請求表單。我已經不止一次地編寫了課程中解釋的代碼,并且我有 99% 的信心我確實正確地編寫了所有內容。但當我嘗試使用請求表單時,我總是收到錯誤&ldquo;405 method not allowed&rdquo;。網上搜索沒有得到任何結果,我找不到解決我的問題的方法。下面是它應該運行的 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>我希望有人能幫助我,我真的不知道如何解決這個問題。提前致謝。
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

在您的 html 文檔中,表單對象沒有 id。在 javascript 代碼中,您正在查找 id = "form" 的對象上的提交事件,但這樣的元素不存在。

所以你只需要改變:

<form id="myForm" method="post">
    <input type="text" id="description" name="description" />
    <input type="submit" name="Create" />
</form>

然后在JS中:

document.getElementById('myForm').onsubmit = ...


查看完整回答
反對 回復 2023-07-29
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號