Javascript 如何使用document.ready在ajax中增加JSON文件
French.json english.jsonJavascript 如何使用document.ready在ajax中增加JSON文件,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,French.json english.json 首先,您需要在文档对象上调用就绪,而不是在元素上调用。因此,将document传递给jQuery,而不是类型选择器“document” 第二: ready()只接受一个函数,因此: 不要使用ready(在您使用的代码中不需要它,因为您没有操作DOM) 调用ready两次,每次传递一个函数 调用ready一次,并将两个函数合并为一个函数 在ready回调中激发两个请求 $(document).ready(function () { $
首先,您需要在
文档
对象上调用就绪
,而不是在
元素上调用。因此,将document
传递给jQuery,而不是类型选择器“document”
第二:
ready()
只接受一个函数,因此:
ready
(在您使用的代码中不需要它,因为您没有操作DOM)ready
两次,每次传递一个函数ready
一次,并将两个函数合并为一个函数在
ready
回调中激发两个请求
$(document).ready(function () {
$.getJSON("French.json", function displayFromJson(french) {
console.log(french.firstName)
});
$.getJSON("english.json", function displayFromJson(english) {
console.log(english.firstName)
});
});
它也可能在linux上运行,其中文件系统路径区分大小写,文件名为
french.json
,可能您没有包含jquery,或者路径是错误的。阅读并创建一个-不要只是转储代码并说它不工作,你应该解释什么是不工作的。你的标题中的“multiple”是指“multiple”而不是“multiply”吗?如果我想在两个getJson之间切换,我会怎么做?
{
"firstName": "Merci",
"lastName": " Claudè"
}
{
"firstName": "Gracias",
"lastName": "Claude"
}
$.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)
});
});
$(document).ready(function () {
$.getJSON("French.json", function displayFromJson(french) {
console.log(french.firstName)
})
$.getJSON("english.json", function displayFromJson(english) {
console.log(english.lastName)
})
});