简体   繁体   English

在文档准备好之前从服务器获取AJAX数据(jQuery)

[英]Get AJAX data from server before document ready (jQuery)

I want take some data from server and write it to global array in JavaScript. 我想从服务器获取一些数据并用JavaScript将其写入全局数组。 Then in document ready I want to use this array to create some new elements (options). 然后在文档就绪中我想使用这个数组来创建一些新元素(选项)。 I should have global array with this data, because after first load client can modify user interface using this data. 我应该有这个数据的全局数组,因为在第一次加载客户端后可以使用这个数据修改用户界面。

$(document).ready(function () {
    UseAjaxQueryForFillGlobalArray();
    MakingInterfaceUsingGlobalArray();       
});

But I have strange behavior, when I debug page, I can see that method MakingInterfaceUsingGlobalArray working first, and just after I get data via AJAX with method UseAjaxQueryForFillGlobalArray and I don't have new interface(html options) with loaded data. 但我有奇怪的行为,当我调试页面时,我可以看到MakeInterfaceUsingGlobalArray方法首先工作,刚刚通过AJAX获取数据后,方法UseAjaxQueryForFillGlobalArray ,我没有新的接口(html选项)加载数据。

If I do like this: 如果我喜欢这样:

UseAjaxQueryForFillGlobalArray();
$(document).ready(function () {     
    MakingInterfaceUsingGlobalArray();       
});

Then in Firefox working fine, but in another web-browsers incorrect in first load (for example go to this page by link). 然后在Firefox工作正常,但在第一次加载时其他网络浏览器不正确(例如通过链接转到此页面)。 But if I refreshing by F5, I have correct user interface which loaded via AJAX to global JS array. 但是如果我用F5刷新,我有正确的用户界面,它通过AJAX加载到全局JS数组。

How to fix it? 怎么解决? Maybe I using totally incorrect way? 也许我使用完全不正确的方式?

Added after comments : 评论后添加

This is my ajax function: 这是我的ajax功能:

function UseAjaxQueryForFillGlobalArray(){
    var curUserId = '<%= Master.CurrentUserDetails.Id %>';
    var curLocale = '<%= Master.CurrentLocale %>';
    $.ajax({
        type: "POST",
        url: "/segment.aspx/GetArrayForCF",
        data: '{"userId":"' + curUserId + '","curLocale":"' + curLocale + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            //here is I doing parse my string from server and fill arrays.     
        }
    });
}

I think that the problem is that you don't know exactly when the first function returns, since it'a asynchronous. 我认为问题是你不知道第一个函数何时返回,因为它是异步的。 So you should use the array in the callback only 所以你应该只在回调中使用数组

function UseAjaxQueryForFillGlobalArray() {
    // make the call
    $.post(url, data, function() {
        // let's be sure that the dom is ready
        $(document).ready(function () {    
            // use the array
            MakingInterfaceUsingGlobalArray();      
        }
    }
}();// invoke the function

It's like reviving this post from the dead, but I had the same problem today, jQuery version greater than 1.6 has this ability: 这就像从死里复活这篇文章,但我今天遇到了同样的问题,jQuery版本大于1.6有这个能力:

https://api.jquery.com/jquery.holdready/ https://api.jquery.com/jquery.holdready/

And I've used it like this: 我用过这样的话:

$.holdReady(true);
var remoteJSONContent = null;
$.getJSON("http://www.example.com/remote.json", function(data) {
    remoteJSONContent = data;
    $.holdReady(false);
});

$(document).ready(function(){
    console.log(remoteJSONContent);
});

Without using holdReady , I was getting null, after, I got the content. 不使用holdReady ,我得到了null,之后,我得到了内容。

For anyone still searching the answer for this. 对于仍在寻找答案的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM