使用服务器端代码从客户端提供的不可靠的API获取JSON输出,并以适当的格式在页面上呈现数据
Using server side code take the JSON output from an unreliable API supplied by a client, and render the data on a page in an appropriate format
所以我遇到这种情况,有人能建议如何处理吗?
使用服务器端代码从不可靠的API获取JSON输出由客户端提供,并以适当的方式在页面上呈现数据总体安排
给定的API URL返回时间序列数据,该数据表示一组网站在30年内在谷歌排名中的表现天。
客户端的API URL为:
https://tools.ayima.com/techtest/api/marketintel
但是,客户端不希望API端点在公共互联网。API也很慢并且非常不可靠,因此您不能只需直接使用JavaScript获取API数据。您需要使用某种服务器端代码,以便首先获取页面的API数据。API速度非常慢,有时只会与503一起失败HTTP状态和一个空正文,所以您的代码应该实现一些缓存或数据库的类型以及重试失败请求的逻辑。对于此测试返回的实际数据永远不会更改,但出于以下目的对于缓存无效,您可以假设数据通常会更改每24小时。
您可以使用相同的后端输出页面/前端代码脚本,直接将API的输出包含在源代码中,或者打印/代理JSON,并在单独的前端使用AJAX获取数据结束代码-任何你觉得最舒服的事情。
我试图用php获取数据,所以原始的API是隐藏的,并在使用angularjs获取后。
这将是我的php来获取数据
<!DOCTYPE html>
<html>
<body>
<?php
$homepage = file_get_contents('https://tools.ayima.com/techtest/api/marketintel');
echo htmlentities($homepage)
?>
</body>
</html>
这将是angularjs部分
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat="x in ayima">
<p>{{ x.domain }}</p>
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://94.136.40.103/~boyisbadnews.com/test/marketintel.php")
.success(function (response) {$scope.ayima = response.marketIntel;})
.error(function(data, status, headers, config) {
// something went wrong
});
});
</script>
</body>
</html>
但无法使其发挥作用。
获取服务器数据的php文件(marketinel.php)必须是:
PHP文件:
<?php
header("Content-Type: application/json");
header("Cache-Control: no-cache");
$homepage = file_get_contents('https://tools.ayima.com/techtest/api/marketintel');
echo $homepage;
?>
HTML文件:我看到您的标记中有一些错误。这里有一个合适的html。
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<meta charset="utf-8" />
<title>Page title</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<div data-ng-controller="customersCtrl">
<ul>
<li data-ng-repeat="x in ayima">
<p>{{ x.domain }}</p>
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://dfjb.webcindario.com/dataAyima.php").success(function(response) {
$scope.ayima = response.marketIntel;
})
.error(function(data, status, headers, config) {
// something went wrong
});
});
</script>
</body>
</html>
相关文章:
- Json数据包含日期和时间格式
- PHP:显示sqlite数据库中的html格式数据,使用tinymce保存
- 元素的内容必须由格式正确的字符数据或标记组成
- 如何通过数据分组将json数据转换为嵌套的json数据格式
- 如何以特定格式存储数据?在WYSWYG中
- 如何将正确的数据格式从SQL传递到PHP
- json格式的国家数据到层次数据
- 如何在系列数据中包含字符串以提取工具提示点格式
- 如何将HTML表单(JSON格式)中的数据分配给变量
- 将JSON数据格式化为excel文件
- 具有任意多条线的D3折线图(以及特定的数据格式)
- 高图表数据导入 JSON - 格式
- HTML5 中的不同数据格式
- Chart.js-折线图格式数据为Lacs/Crores
- 使用DimpleJS和DSV格式数据绘制图表
- 条件格式数据表DT R
- 使用客户端Internet从客户端浏览器读取返回Xml格式数据的Url,而不向服务器发送请求
- 格式数据从json文件到树图值图表
- 美元.ajax发送文件格式数据和变量
- 将格式数据jquery从long更改为short