Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用document.ready在ajax中增加JSON文件_Javascript_Jquery_Json_Ajax - Fatal编程技术网

Javascript 如何使用document.ready在ajax中增加JSON文件

Javascript 如何使用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 () { $

French.json

english.json


首先,您需要在
文档
对象上调用
就绪
,而不是在
元素上调用。因此,将
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)
          })
    });