使用XHR对象的GET请求,找不到URI组件名称

时间:2017-10-13 15:46:47

标签: javascript python ajax django

我试图创建一个文本框,在输入一个字符后,查询服务器。

我收到以下错误,但我不明白为什么:

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视图,它将处理来自XHR对象的get-request:

def get_lifts(request):
    if request.method == 'GET':
        search_str = request.GET['lift_string']
        return HttpResponse('Received lift_string')

在我看来,我在javascript中添加到lift_string的{​​{1}} URI组件名称无法找到?为什么呢?

编辑:url的内容:

添加print语句进行查看:

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')

1 个答案:

答案 0 :(得分:2)

您忘记将addURLParam的结果分配给url,如下所示:

url = addURLParam(url, document.getElementById("lift").name, document.getElementById("lift").value);