javascript - 设置“无”而不是所需数据
问题描述
我想根据另一个字段的值设置一个字段值(使用 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)
解决方案
如果您调用xhttp.send(x)
,则不再知道变量的名称。您需要对此进行编码,例如:
xhttp.send("x="+encodeURIComponent(x));
推荐阅读
- javascript - 带有 Javascript 的响应式 Bootstrap 进度条
- python - 矢量化和广播
- html - links.append() 不会将所有图像链接显示为列表
- java - 如何对地图进行排序
? - java - 未授权 (401) 异常,在 java 中使用 EWS 发送邮件时
- c++ - 如何避免同一个列表上的多线程中的空指针?
- php - 如何在 WooCommerce 中遍历订单商品数量
- flutter - 我想在我的颤振应用程序中通过 onPressed 方法生成卡片。谁能给我一个路线图...?
- ubuntu - 如何使用共享文件的确切版本安装 OpenMPI?
- regex - 匹配一个单词后跟任何数字,但有例外