[英]How to fetch data from Python function to JavaScript | Django
在這里,我試圖從get_bot_response
function 獲取數據(“好的,我們拭目以待。”)。 但是每次我從 JavaScript 調用它時,它都會顯示500 (Internal Server Error)
。 這是我嘗試的方法,
視圖.py
def get_bot_response(request):
userText = request.GET.get('msg')
return "Okay, we\'ll see."
網址.py
urlpatterns = [
...
path('get/', views.get_bot_response, name='get'),
]
在模板腳本中
function botResponse(rawText) {
// Bot Response
$.get("/get", { msg: rawText }).done(function (data) {
const msgText = data;
appendMessage("", "msgText");
});
}
以及在http://127.0.0.1:8000/get/
中,它顯示Error: 'str' object has no attribute 'get'
您必須從您的視圖中返回一個HttpResponse
(或其子類)。 你不能只返回一個字符串
from django.http import HttpResponse
def get_bot_response(request):
userText = request.GET.get('msg')
return HttpResponse("Okay, we\'ll see.")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.