如何在my中获取XMLHttpResponse发送数据视图.py?

2024-09-26 22:09:33 发布

您现在位置:Python中文网/ 问答频道 /正文

在模板中,我使用XMLHttpResponse发送数据。你知道吗

我的代码如下:

...
<input type="button" value="ajax1" onclick="ajax1()">


<script>

    function ajax1(){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/ajax1/', true);  
        xhr.send("name=root;pwd=123");  // send data
    }

</script>

但是我如何在views.py中接收数据呢?你知道吗

在我的views.py中:

def ajax1(request):
    print request.GET.get('name'), request.GET.get('pwd') # all is None.
    return HttpResponse('ajax1')

你看,我用request.GET.get(param_key)获取数据失败了。你知道吗

如何在my中获取XMLHttpResponse发送数据视图.py?你知道吗


Tags: 代码namepysend模板getrequestpwd
1条回答
网友
1楼 · 发布于 2024-09-26 22:09:33

您应该知道XMLHttpResponse的send()方法是send-request-body。 您的请求方法是GET。所以你不能传递数据。你知道吗

您尝试使用post方法传递数据,如下所示:

function ajax1(){
    var xhr = getXHR();

    xhr.onreadystatechange = function(){  
        if (xhr.readyState == 4) {   

            console.log(xhr.responseText); 

            var json_obj = JSON.parse(xhr.responseText);
            console.log(json_obj);

        }
    }; 

    xhr.open("POST", "/ajax1/", true);   
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset-UTF-8"); // add the request header

    xhr.send("name=root; pwd=123;");  // send data
}

相关问题 更多 >

    热门问题