Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
Jquery 确保始终加载JSON内容-有时会失败_Jquery_Ajax_Json - Fatal编程技术网

Jquery 确保始终加载JSON内容-有时会失败

Jquery 确保始终加载JSON内容-有时会失败,jquery,ajax,json,Jquery,Ajax,Json,我有一个微型站点,它有一个base index.html,并使用Ajax加载到其他html文件中,在这些加载的html文件中,我还加载JSON文件中的内容。我想知道是否有办法确保总是加载这些内容,因为有时我会发现HTML加载了,但JSON内容没有加载:当用户单击进入新层时,我使用AJAX调用JSON(loadContent()),在这个函数中,我调用另一个函数,该函数以特定HTML元素加载JSON内容(showInfo()) 也许我的代码不够健壮,需要修改 示例代码: $(document).o

我有一个微型站点,它有一个base index.html,并使用Ajax加载到其他html文件中,在这些加载的html文件中,我还加载JSON文件中的内容。我想知道是否有办法确保总是加载这些内容,因为有时我会发现HTML加载了,但JSON内容没有加载:当用户单击进入新层时,我使用AJAX调用JSON(loadContent()),在这个函数中,我调用另一个函数,该函数以特定HTML元素加载JSON内容(showInfo())

也许我的代码不够健壮,需要修改

示例代码:

$(document).on('click', 'a.loser-next-button', function(e){
        e.preventDefault();
        $('#mch-overlay-content').html('');
        $('#mch-overlay-content').load("layer_recommend.html");
        loadContent();
});

function loadContent(){ 
    $.ajax({
        url: "json/" + language + "/content.json",
        data: "nocache=" + Math.random(),
        type: "GET",
        cache : false,
        contentType: "application/json",
        dataType: "json",
        success: function(source){
            data = source;
            showStartpage(data);
            showInfo(data);
        },
        error: function(data){
            alert("Failed to load content");
        }
    }); 

}

function showInfo(){
    $(".start .text3").html(data[language]['startpage']['text3']);
    $(".start .text4").html(data[language]['startpage']['text4']);
    $(".start .text5").html(data[language]['startpage']['text5']);
    $(".start .text6").html(data[language]['startpage']['text6']);
    $(".start .text7").html(data[language]['startpage']['text7']);
                ...................................

1.
语言如何设置?是否保证在请求完成时具有值?2.当函数不带任何参数时,为什么要将
数据
传递到
showInfo
?语言设置为“de”之类的值,并且有一个保证值。我将数据传递给showInfo,以将ajax调用返回的任何内容传递给该函数。好的,
showInfo
没有指定参数,因此完全不需要在调用中传递它。。。至于失败的原因-浏览器开发人员对该请求有何看法?