-3

我可以使用以下 js 代码在 chrome 控制台中显示从服务器中的 json 文件读取的数据:

    function leer2(){
      var invite_url = "initval";
      $.getJSON("https://graph.facebook.com/btaylor",{format: "json"}).done( 
            function(person){
            invite_url = person.locale;
            console.log(invite_url);
        });
    };

并通过事件调用此 js 函数

    <button id="leer" onclick="leer2()">Leer BaseDat</button>

让我做两个问题:首先,它是不是像我正在做的那样读取我的 json 文件的不安全方式?,第二。如何将 *invite_url* 值放入 /body 标签并使用它?

4

2 回答 2

0

如果您想person.locale在 HTML 的正文中使用该值,您可以创建一个 div 或一个结构来保存返回的值并使用 ID 定位它,如下所示:

function leer2(){
  var invite_url = "initval";
  $.getJSON("https://graph.facebook.com/btaylor",{format: "json"}).done( 
        function(person){
        invite_url = person.locale;
        console.log(invite_url);

        // inserts the returned value into the div
        $("#invite_url_holder").text(invite_url);
    });
};

<div id="invite_url_holder"></div>
于 2013-07-08T17:14:32.497 回答
0

要使用 JQuery 将 HTML 直接添加到文档中,只需在函数中使用以下代码:

$(element_you_want_to_add_html_to).text(the_inner_text);

或者

$(element_you_want_to_add_html_to).html(the_inner_text);
于 2013-07-08T17:21:29.727 回答