我正在尝试创建一个文本框,在其中输入字符后,查询服务器。
我收到以下错误,但我不明白原因:
Traceback (most recent call last):
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/datastructures.py", line 83, in __getitem__
list_ = super(MultiValueDict, self).__getitem__(key)
KeyError: 'lift_string'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/workout/workoutcal/views.py", line 71, in get_lifts
search_str = request.GET['lift_string']
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/datastructures.py", line 85, in __getitem__
raise MultiValueDictKeyError(repr(key))
django.utils.datastructures.MultiValueDictKeyError: "'lift_string'"
当我在此文本框中输入字符时,我得到它:
<input id="lift" name="lift_string" type="text" onkeyup="getLifts()">
这是文本框的关联 JavaScript:
function addURLParam(url, name, value){
url += (url.indexOf("?") == -1 ? "?" : "&");
url += encodeURIComponent(name) + "=" + encodeURIComponent(value);
return url;
}
function getLifts(){
var xhr = createXHR();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
document.getElementById("xhrPar").innerHTML = xhr.responseText;
} else {
document.getElementById("xhrPar").innerHTML = "Request was unsuccessful: "+xhr.status;
}
}
};
var url = "http://localhost:8000/workoutcal/get_lifts";
addURLParam(url, document.getElementById("lift").name, document.getElementById("lift").value);
xhr.open("get", url, false);
xhr.send(null);
}
这里是 Django View ,它将处理来自 XHR 对象的 get 请求:
def get_lifts(request):
if request.method == 'GET':
search_str = request.GET['lift_string']
return HttpResponse('Received lift_string')
在我看来,我在 javascript 中添加到 url
的 lift_string
URI 组件名称找不到?为什么?
编辑:request.GET
的内容:
添加打印语句以查看:
def get_lifts(request):
if request.method == 'GET':
print("CONTENT OF request.GET: "+str(request.GET))
search_str = request.GET['lift_string']
return HttpResponse('Received lift_string')
导致此输出:
CONTENT OF request.GET: <QueryDict: {}>
最佳答案
您忘记将 addURLParam
的结果分配给 url
,如下所示:
url = addURLParam(url, document.getElementById("lift").name, document.getElementById("lift").value);
关于javascript - 带有 XHR 对象的 GET 请求,未找到 URI 组件名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46733596/