【发布时间】:2017-12-18 16:03:09
【问题描述】:
这是 html 文件
<html>
<head>
<title>"api call"</title>
</head>
<body>
<div id="demo">
<script>
function list() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET","192.168.0.101:8000/students/",true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.setRequestHeader("Authorization", "Token ad4140b1caa4f98160bdc979a71a7215ae5972fe");
xhttp.send();
var1 response = JSON.parse(xhttp.responseText);
document.write(var1);
}
</script>
<button type="button" onclick="list()">click to get the list</button>
</div>
</body>
</html>
当我在浏览器中运行时,请求未发送(我在后端的跟踪球中看不到任何内容) 如果在浏览器和邮递员中使用,该 url 可以正常工作 this is the screenshot of postman request and response
后端在 django 中
这是我在控制台中遇到的错误
var1 response = JSON.parse(xhttp.responseText);
错误:未捕获的语法错误:意外的标识符
【问题讨论】:
-
您收到什么错误?
-
@Alexandru-IonutMihai 我已经编辑了,你可以在最后看到
-
看看我的回答。
-
@Alexandru-IonutMihai 我现在在控制台中收到此错误 Uncaught ReferenceError: list is not defined at HTMLButtonElement.onclick (javascriptapi:21)
标签: javascript ajax django web