首页 > 解决方案 > 设置“无”而不是所需数据

问题描述

我想根据另一个字段的值设置一个字段值(使用 AJAX)。在这里,我试图设置相同的值,但它被设置为“无”而不是实际值。

发送请求的脚本:

<script>
  function getCustomerName() {
    var x = document.ipwhitelistindex.AWSID.value;
    console.log(x)
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.ipwhitelistindex.CUSTNAME.value = this.responseText;
      }
    };
    xhttp.open('POST', 'getcustomernamefromexcel', true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send(x);
  }
</script>

在这里,我发送 x。我可以在控制台中看到确切的值,但它在页面上设置为 None。

Python :

 def getcustomernamefromexcel(request):
    value = request.POST.get('x')
    return HttpResponse(value)

标签: javascriptpythonhtmldjangoajax

解决方案


如果您调用xhttp.send(x),则不再知道变量的名称。您需要对此进行编码,例如:

xhttp.send("x="+encodeURIComponent(x));

推荐阅读