javascript - 未按预期获得虚拟 json 响应,但出现错误

标签 javascript html ajax

根据以下代码,我必须获得一个虚拟 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/

相关文章:

javascript - 使用ajax从文件中读取数据

javascript - 如何在 Javascript 中将封闭函数作为其自己的递归回调传递?

javascript - 使用下划线或 lodash 将一种 JSON 结构转换为另一种

javascript - 使用 lodash 提取混合对象/数组的叶子

javascript - 通过单击表格标题根据单元格背景颜色对表格进行排序

javascript - CircleType 文本在 rotateY 之后删除空格

jquery - Ajax 调用通过单个请求绑定(bind)多个 HTML 控件

javascript - 在javascript中更新总价

javascript - 在 MVC 5 中刷新局部 View Div

html - 无法在 Angular 9 中使用 *ngFor 显示选择列表中的选项