我正在開發一個網站,我的首要任務之一是具有自動完成功能等的動態搜索表。我發現這可能使用ajax和 javascript。每當用戶在搜索字段中鍵入內容時,我已經實施了一個觸發器。當前的問題是從數據庫中獲取數據,現在我收到 404 錯誤,并且沒有返回任何數據。代碼:views.pydef search_bib(request): if request.method == "POST": search_str=json.loads(request.body).get('searchText') bib = Bibrest51.objects.all().filter( autor__starts_with=search_str) | Bibrest51.objects.all().filter( autor__icontains=search_str) data = Bibrest51.objects.all() return jsonResponse(list(data), safe=False)記者:const searchField = document.querySelector("#searchField");searchField.addEventListener("keyup", (e) => { const searchValue = e.target.value; if (searchValue.trim().length > 0) { console.log("searchValue", searchValue); fetch("bibliografia-search", { body: JSON.stringify({ searchText: searchValue }), method: "POST", }) .then((res) => console.log(res)) .then((data) => { console.log("data", data); }) .catch((e) => { console.log(e); }); }});任何幫助將不勝感激,我是該網站的新手,我希望這篇文章沒有做錯任何事。對于改進我的帖子的反饋也很感激 - 謝謝!
在 Django 上使用 Javascript 正確獲取查詢
www說
2023-04-01 17:28:15