Php 在HTML中显示JSON数据
我正在使用intel XDK开发一个混合应用程序。在应用程序中,我使用ajax请求我的PHP服务器。服务器将使用json数据进行响应 这是来自服务器的json示例Php 在HTML中显示JSON数据,php,jquery,html,json,hybrid-mobile-app,Php,Jquery,Html,Json,Hybrid Mobile App,我正在使用intel XDK开发一个混合应用程序。在应用程序中,我使用ajax请求我的PHP服务器。服务器将使用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":
[{
"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。我只使用了一个用户产品的每个。然后,为数据将显示的每个类创建一个样式。
样本:
服务器应提供类似以下内容的json:
<?php
$data = array();
foreach(products as product){
array_push($data, $product);
}
header('Content-Type: application/json');
echo json_encode($data);
JS-Bin
通过AJAX获取
产品形象
产品名称
日期
利润
投资
我被你的问题弄糊涂了。你说PHP服务器输出JSON,但是你的示例包含PHP来输出HTML?仅使用HTML是不可能的,你可以使用ajax和循环来获得所需的效果。你是否从第三方服务器请求数据,并在你的页面中使用PHP和HTML?很抱歉,各位混淆了,我有现有的程序(PHP),现在我正在开发混合应用程序版本(HTML、CSS、jquery)。所以我只是向服务器发送请求以获取用户的数据等@mulquin@parag是的,我正在使用第三方服务器。我在服务器上有应用程序请求的API。我只是在客户端使用HTML、CSS和jquery,这是应用程序。我的代码有错误data.forEach不是一个函数
@c.k你做过ajax吗。我还没有写ajaxthere@c.k我也用ajax代码更新了答案。看一看,我想我的密码错了。我是这样做的:@c.k删除第12行,不是必需的
$("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);
}
});
});
<?php
$data = array();
foreach(products as product){
array_push($data, $product);
}
header('Content-Type: application/json');
echo json_encode($data);