- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有来自 Guardian API 开放平台的 JSON 格式的数据,我想在 jquery 中解析它,目前我一直在尝试将结果显示到我的 HTML div 上。
数据格式如下:Guardian JSON results
我尝试使用的代码如下
function processFootballData(footballData){
footyStuff = footballData;
var thisContainer = document.getElementById( "results" );
var listTmp = document.createElement( "ul" );
var tmpList = "";
for( var i=0; (i<footyStuff.results[0].length) && (i<100); i++ ) {
if( tmpList.length <= 0 ) {
tmpList = footyStuff.results[0][ i ];
}
else {
tmpList = tmpList + "," + footyStuff.results[0][ i ];
}
}
var footballURL = "http://content.guardianapis.com/search?q=football&format=json&api-key=ky5zy8mds5r25syu36t9kmzj";
$.getJSON( footballURL,
function( thisData ) {
var data = thisData;
for( var key in data ) {
var thisSublist = document.createElement( "ul" );
thisSublist.setAttribute('style', "border-bottom: 1px solid #000; width: 80%;");
var thisItem = document.createElement( "li" );
var footyResults = data[key].results[0];
if( data.hasOwnProperty( key ) ) {
var duyList = document.createElement("li");
duyList.setAttribute('style', "padding-bottom: 10px;margin-top:-15px;margin-left:53px;font-size:12px;");
duyFooty = document.createTextNode(footyResults);
duyList.appendChild(duyFooty);
thisItem.appendChild(duyList);
}
thisItem.appendChild( thisSublist );
}
listTmp.appendChild( thisItem );
}
thisContainer.appendChild( listTmp );
});
}
最佳答案
您需要使用 jsonp
请求,因为 Guardian API 会阻止跨域请求。使用 JQuery .ajax
与数据类型:jsonp
:
$.ajax({
url: footballURL,
dataType: 'jsonp',
success: function( thisData ) {
var data = thisData;
// etc ...
}
});
你的 DOM 生成 Javascript 有点困惑……但是编写这种代码很容易迷失方向。我强烈建议使用某种微模板引擎来处理数据到 HTML 的转换。
<小时/>Here's an example如何使用 Mustache.js 执行此操作.
// create HTML template with data tags
var template = "<ul>{{#results}}<li><ul><li><a href='{{webUrl}}'>{{webTitle}}</a></li></ul></li>{{/results}}";
// render output
var output = Mustache.render(template, thisData.response);
// add to the DOM
$("#results").html(output);
<小时/>
Here's the same example使用Underscore.js 。相同的想法,但不同的实现,让您可以将模板编写为标记:
<script type='text/template' id='article-template'>
<% _.each(results, function(article) { %>
<ul>
<li style="border-bottom: 1px solid #000; width: 80%;">
<a href='<%= article.webUrl %>'><%= article.webTitle %></a>
</li>
</ul>
<% }); %>
</script>
以及要渲染的脚本:
var template = _.template($("#article-template").html());
var output = template(thisData.response);
$("#results").html(output);
关于javascript - Guardian API,JSON解析jquery/javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13276430/
我正在启动一个具有复杂权限结构的应用程序,这将不可避免地由用户自己管理。我在模型中有以下权限: class Meta: permissions = ( ('can_view', 'Vi
我想知道是否可以在没有用户模型的情况下使用 Guardian,以及如何使用它。 我只将 Phoenix 用于服务器、微型 API 和 channel 。我在浏览器中有一个带有 PouchDB 的数据库
谁能推荐一个使用 django-guardian 的好的开源应用程序?我在理解 API 方面没有问题,但我很想看到一个示例来了解实现最佳实践(数据库设计等) 最佳答案 如果您克隆 django-gua
我正在使用 django-guardian 来检查用户的对象权限。在我的特殊情况下,我扩展了用户模型。在我的 models.py 中,我扩展了这样的用户模型: enter image descript
我正在使用 django-guardian 来检查用户的对象权限。在我的特殊情况下,我扩展了用户模型。在我的 models.py 中,我扩展了这样的用户模型: enter image descript
尝试搜索监护人模式,但我找不到任何可以回答我问题的东西,所以我又来了。在我正在阅读的书中,作者使用了一行他称之为“监护人模式”的代码,但我并不真正理解它是如何工作的。这是示例,如果有人能向我解释一下,
我已按照 django-guardian 文档中的用户指南来设置 django-guardian。这使我能够控制某个组是否可以查看特定类(class)。这是 django-guardian 文档中的示
我有两个模型: class ContactGroup(models.Model): name = models.CharField(max_length=40) class Meta:
我有一个父类(super class),它带有带有 finalize() 方法的伸缩构造函数。为了防止子类忘记调用 super.finalize,我编写了一个终结器守护程序(EJ Item 7),就像
我已将 django-guardian 的行级权限添加到我的项目中。 从设置来看,似乎一切正常: 已创建监护人特定表(guardian_groupobjectpermission、guardian_u
我正在使用 django-guardian为了管理每个对象的权限。 对于给定的用户,我授予对一个对象的所有权限: joe = User.objects.get(username="joe") myta
根据Akka docs on the System Guardian , 如果顶级 Actor 抛出 Exception ,它将无限期地重新启动(有 2 个异常(exception) - 没有双关语)
所以我有来自 Guardian API 开放平台的 JSON 格式的数据,我想在 jquery 中解析它,目前我一直在尝试将结果显示到我的 HTML div 上。 数据格式如下:Guardian JS
我正在开发一个实现用户身份验证的网站(使用 Comeonin 和 Guardian)。 我正在实现电子邮件验证。我想我可以利用 Guardian 中的功能来使用 JWT token 生成 url。根据
我在从 The Guardian 的新闻 API 获取文章文本时遇到问题。我有一个类似这样的网址:http://content.guardianapis.com/search?section=tech
我正在创建一个应用程序,它从《卫报》获取新闻。该文档不清楚依赖关系。任何地方都没有这方面的信息。预先感谢您 https://github.com/matarrese/content-api-the-g
当我在 Django 中创建用户时,他没有权限: In [7]: u = User.objects.create(username='aoeu') In [12]: u.user_permission
我正在寻找一个简单的示例,说明如何以与 Guardian 应用程序(包括左侧图像)相同的方式在屏幕上显示数据,我从 php 页面中提取数据,如屏幕截图所示以下。我在互联网上找不到任何符合此描述的示例。
我目前正在对所有对象进行非常笨拙的循环,但这会变得很慢: videos = Video.objects.all() video_list = [] for video in videos: c
有 from guardian.shortcuts import get_objects_for_user 但是关于 from guardian.shortcuts import get_users_
我是一名优秀的程序员,十分优秀!