【发布时间】:2021-03-10 19:37:47
【问题描述】:
当控制来自文本字段时,我正在调用一个脚本,该脚本又调用一个 python 函数来返回响应。调用函数时出现错误::POST http://127.0.0.1:8000/getcustomernamefromexcel 403 (Forbidden)
包含脚本的 HTML 文件:
{% extends 'base.html' %}
<html>
<body>
{% block content %}
<script type ="text/javascript">
function getCustomerName() {
var x = document.ipwhitelistindex.AWSID.value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.ipwhitelistindex.getElementById("CUSTNAME").innerHTML = this.responseText;
}
};
xhttp.open('POST', 'getcustomernamefromexcel', true);
xhttp.send(x);
}
</script>
<h1>IP Whitelisting</h1>
<form name = "ipwhitelistindex" action = 'ipwhitelisting' method = "post">
{% csrf_token %}
<center>
<table>
<tr><td>Enter AWS Account Id </td><td>:</td><td><input type = 'text' name = AWSID onblur="getCustomerName()"></td></tr>
<tr><td>Customer Name </td><td>:</td><td><input type = 'text' name = CUSTNAME style = "background-color : lightgrey" readonly></td></tr>
<tr><td>Enter list of IPS seperated with(,) </td><td>:</td><td><input type = 'text' name = IPS></td></tr>
<tr><td>Enter description </td><td>:</td><td><input type = 'text' name = DESC></td></tr>
</table><br>
<input type = 'submit' value = 'submit'>
</center>
</form>
{% endblock %}
</body>
</html>
python 代码:
def getcustomernamefromexcel(request):
return HttpResponse('html')
【问题讨论】:
-
尝试使用
JsonReponse而不是HttpResponse。例如,试试这个:return JsonResponse({ 'msg': 'html' })。从django.http导入JsonResponse。
标签: javascript python html django ajax