如何将json格式的数组数据从jquery返回到html
How to return json formated array data from jquery to html
从FB获取数据的示例。。。
$(document).ready(function(){
var name;
var link;
var gender;
var id;
$.getJSON( "http://graph.facebook.com/4/", function( json ) {
name = json.name;
link = json.link;
gender = json.gender;
id = json.id;
var person = {name:name,link:link,gender:gender,id:id};
console.log(person);
// This gives me exactly what I need but only in console view.
$('html').text(person);
// This just writes [object Object] inside my window
return person;
});
});
我很感激你的帮助,我知道这是最基本的,但现在我的大脑没有正常工作:''
I也建议使用某种模板系统,如下划线、车把、mustasche等。但是,如果使用有限,您可以自己动手,而不是将整个框架用于一个模板。
HTML中需要一些占位符。在这个例子中,我使用了Mustasche.js风格的占位符。示例:
<html>
<body>
<ul>
<li>name: {{name}}</li>
<li>gender: {gender}</li>
<li>link: {{link}}</li>
<li>id: {{id}}</li>
</ul>
</body>
</html>
然后我们想用适当的值替换占位符,可以这样做:
...
$.getJSON( "http://graph.facebook.com/4/", function( json ) {
name = json.name;
link = json.link;
gender = json.gender;
id = json.id;
var person = {name:name,link:link,gender:gender,id:id};
// Get the current HTML from the selector
var template = $("html").html();
// Replace each placeholder w/ the correct thing
template = template.replace("{{name}}", name);
template = template.replace("{{link}}", link);
template = template.replace("{{gender}}", gender);
template = template.replace("{{id}}", id);
// Write the new HTML to the selector
$("html").html(template);
return person;
});
...
我建议使用类似_.template()
的模板函数
var compiled = _.template("<p>Name: <%= name %></p>");
compiled(person);
//<p>Name: Theresa</p>
http://underscorejs.org/#template
相关文章:
- jquery返回边距为0的绝对中心
- 如何使用jquery返回php-json数组对象
- Firefox使用JQuery返回不正确的JSON
- 当jQuery返回错误时,如何在jQuery中重新发送ajax请求
- jquery返回ajax响应和用户定义的变量到函数
- 如何从ajax调用JQuery返回数据
- 单击按钮时Jquery返回到父函数
- jQuery返回prevObject..有时
- 使用String.FromCharCode时,jQuery返回未定义的结果
- jQuery返回多个函数;不起作用
- 正在读取嵌套json,jquery返回undefined
- IE使用类jquery返回相同的最后一个单选按钮值
- 在数组中循环,jQuery返回undefined
- 从php向jquery返回jsonencode错误
- .children()不适用于jquery返回的指定索引
- HTML 复选框 Click() 使用 jQuery 返回 FALSE 或 TRUE,而不是 READONLY 或 DI
- 使用 jQuery 返回除 Drupal7 表单之外的所有点击事件的消息
- 排序 Jquery 返回的 HTML 元素
- jQuery 返回空字符串作为 ID 表示
- jQuery返回所有页面已禁用,然后启用