我正在使用英特尔 XDK 开发混合应用程序。在我的 PHP 服务器上使用 ajax 请求的应用程序。服务器将返回 json 数据。
这是我来自服务器的示例 json。
[{
"id":"11",
"user_id":"8000",
"product":"Shoes A",
"quantity":"1",
"date_open":"2015-01-04",
"paid":"1",
"harvested":"",
"reinvest":null,
"profit":null,
"investment":"3000"
},
{
"id":"12",
"user_id":"8000",
"product":"Shoes B",
"quantity":"1",
"date_open":"2015-03-01",
"paid":"1",
"harvested":"",
"reinvest":null,
"profit":null,
"investment":"1500"
}]
这里每个用户的产品数量不同,有些没有产品,有些有 1、2...10 等产品。因此,根据用户的产品数量,最好的展示方式是什么。这样数据/项目就会全部组织起来,并在页面加载时很好地显示图像。
应该自动显示:
<强>|产品图片 |产品名称 |日期 |利润 |投资
我的 html 页面/css 样式应该设置什么?或者任何我应该了解的更多信息。
在我现有的系统上使用 PHP。我只是使用用户产品的foreach。然后为每个将显示数据的类设置样式。 示例:
foreach(products as product){
?>
<div class="img"></div>
<div class="productname">echo product['product'];</div>
<?
}
所以我在考虑是否可以像我在 PHP 中所做的那样在 html 中显示它。感谢您的帮助。
编辑:我在客户端的 ajax 调用:
$("button").click(function(){
$.ajax({
type: 'POST',
url: "http://www.sample.com/app/user-data.php",
crossDomain: true,
dataType: 'json',
data: { user_token: user_token },
success: function(data, status, jqXHR) {
//console.log(data); //`this is displaying only as object why?`
//console.log(status);
console.log(JSON.stringify(data)); //to string
},
error: function(xhr, ajaxOptions, thrownError) {
alert(ajaxOptions + " " + thrownError);
}
});
});
强>最佳答案
服务器应该提供像这样的 json:
<?php
$data = array();
foreach(products as product){
array_push($data, $product);
}
header('Content-Type: application/json');
echo json_encode($data);
您应该通过 ajax 获取数据,然后更新 DOM:
var dummy_data = [{
"id": "11",
"user_id": "8000",
"product": "Shoes A",
"quantity": "1",
"date_open": "2015-01-04",
"paid": "1",
"harvested": "",
"reinvest": null,
"profit": null,
"investment": "3000"
},
{
"id": "12",
"user_id": "8000",
"product": "Shoes B",
"quantity": "1",
"date_open": "2015-03-01",
"paid": "1",
"harvested": "",
"reinvest": null,
"profit": null,
"investment": "1500"
}
];
function addData(data) {
data.forEach(function(row) {
var str = '<tr>';
str += '<td>' + row.id + '</td>';
str += '<td>' + row.product + '</td>';
str += '<td>' + row.date_open + '</td>';
str += '<td>' + row.profit + '</td>';
str += '<td>' + row.investment + '</td>';
str += '</tr>';
$('#data_tbl').append(str);
});
}
$("#fetch_btn").click(function() {
//do ajax here and load data
/*
$.getJSON("get_data.php", function(result) {
addData(result);
});
*/
//for demo calling the function with dummy data
addData(dummy_data);
});table,
th,
td {
border: 1px solid black;
}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="fetch_btn">FETCH BY AJAX</button>
<table id="data_tbl">
<tr>
<th>image of product</th>
<th>name of product</th>
<th>date</th>
<th>profit</th>
<th>investment</th>
</tr>
</table>
</body>
</html>
关于php - 在 HTML 中显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237681/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":