根据以下代码,我必须获得一个虚拟 json 响应,但它却向我显示了一个错误。为什么?
<html>
<head></head>
<body>
<script>
var root = 'https://jsonplaceholder.typicode.com';
$.ajax({
url: root + '/posts/1',
method: 'GET'
}).then(function(data) {
console.log(data);
});
</script>
</body>
</html>
引用: https://jsonplaceholder.typicode.com/
提前致谢。
最佳答案
错误是:
{ "message": "Uncaught ReferenceError: $ is not defined",
"filename": "https://stacksnippets.net/js", "lineno": 17, "colno": 1 }
所以你需要添加 jquery 作为脚本,如下所示:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
您还可以查看 jquery page
关于javascript - 未按预期获得虚拟 json 响应,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45976099/