【问题标题】:Load JSON Data into Document Ready function将 JSON 数据加载到 Document Ready 函数中
【发布时间】:2013-10-19 18:32:17
【问题描述】:

这听起来像是一个愚蠢的问题。我在我的 HTML 页面中在我的 HTML 页面中的“/body”语句结束之前添加了以下内容:

<script src="http://test.com/api/?json=recentstories&callback=myPosts" type="text/javascript"></script>

我希望将 myPosts 中的数据加载到变量中:

$(document).ready( function () {
var  loadeddata = //load the data here
}

或者我应该在文档就绪函数中使用 $.getJSON 并从我的 HTML 页面中删除 javascript?

对不起,如果这不是一个正确的问题。

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    好的,我让它像这样工作:

        $(document).ready( function () {
        var data = $.getJSON("http://test.com/api/?json=recentstories", function(data) {
        console.log(data.posts);
    });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      相关资源
      最近更新 更多