javascript - 如何使用 document.ready ajax 乘以 JSON 文件
问题描述
$("document").ready(
function () {
$.getJSON("French.json", function displayFromJson(french) {
console.log(french.firstName)
})
},
function () {
$.getJSON("english.json", function displayFromJson(english) {
console.log(english.lastName)
})
});
法语.json
{
"firstName": "Merci",
"lastName": " Claudè"
}
英语.json
{
"firstName": "Gracias",
"lastName": "Claude"
}
解决方案
首先,您需要调用对象ready
而document
不是<document>
元素。所以传递document
给 jQuery,而不是类型选择器"document"
。
第二:
ready()
接受一个且仅一个函数,因此:
- 不要使用
ready
(在您使用的代码中不需要它,因为您没有操作 DOM) - 调用
ready
两次,每次传递一个函数 - 调用
ready
一次,将两个函数合二为一
$.getJSON("French.json", function displayFromJson(french) {
console.log(french.firstName)
});
$.getJSON("english.json", function displayFromJson(english) {
console.log(english.firstName)
});
$(document).ready(function () {
$.getJSON("French.json", function displayFromJson(french) {
console.log(french.firstName)
});
});
$(document).ready(function () {
$.getJSON("english.json", function displayFromJson(english) {
console.log(english.firstName)
});
});
$(document).ready(function () {
$.getJSON("French.json", function displayFromJson(french) {
console.log(french.firstName)
});
$.getJSON("english.json", function displayFromJson(english) {
console.log(english.firstName)
});
});
推荐阅读
- r - R 具有非零观测子集的数据帧的百分位数
- google-sheets - sum 逗号分隔的整数字符串
- c# - AutoMapper 与 Google Protocol Buffers 3 的务实使用
- python - 创建对象的匿名幻灯片问题
- arrays - 在gamemaker中并排显示2个选项?
- dictionary - 如何在javascript中检索字典对象会话中的值
- javascript - 会话用户有时具有价值有时未定义
- android - 使用 android studio 创建自定义形状?
- python - 过滤包含 NaN 值的数据框,结果为空数据框
- javascript - 鼠标滚轮触发时多次触发函数