javascript - 使用 ajax 在 javascript 中使用 json 的数据
问题描述
我在我的 html 文档中使用 json 文件的数据时遇到问题。
在顶部,您可以看到我得到的错误。
我将此文件创建为 json 文件 New.json :
[
{ "firstname":"Thomas", "lastname":"Anderson" },
{ "firstname":"Agent", "lastname":"Smith" },
{ "firstname":"Agent", "lastname":"Brown" }
]
我尝试使用此代码从我的 json 文件中获取数据
<script type="application/javascript">
$(document).ready(function() {
$("div").click(function () {
$.ajax({
type: 'GET',
url: './New.json',
dataType: 'json',
success: function(data){
window.alert(data);
},
});
});
});
</script>
谢谢你的帮助 :)
解决方案
您的 URL 可能需要是: url: './New.json',
推荐阅读
- javascript - 使用 JavaScript 在 HTML 表格中添加行
- java - 两个 DateTime 对象之间的差异(以分钟为单位)
- vue.js - Vue 语法错误:Unexpected Token
- java - 我的 outputStream 声明出现 VariableDeclaratorID 预期异常
- reactjs - 如何在不同的 URL 路由之间保存 redux 状态?
- java - 将原始文本添加到 SOAP 标头
- c - 找到时无法返回树节点
- javascript - 如果 setAttribute() 是定义的变量,它不会“记住”函数中分配给它的 ID 的值吗?
- https - 带有letsencrypt的KeystoneJS - 需要证书文件
- netlify - 是否可以在 netlify.toml 文件中设置 access_token ?