Using Fetch to rewrite JSON
截图如下:

html代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width"> <title>Fetch json example</title>
<style type="text/css">
html {
font-family: sans-serif;
} ul {
list-style-type: none;
display: flex;
flex-flow: column;
align-items: flex-start;
} li {
margin-bottom: 10px;
background-color: pink;
font-size: 150%;
border-top: 3px solid pink;
border-bottom: 3px solid pink;
box-shadow: 5px 5px 5px rgba(0,0,0,0.7);
} strong {
background-color: purple;
color: white;
padding: 0 8px;
border-top: 3px solid purple;
border-bottom: 3px solid purple;
text-shadow: 2px 2px 1px black;
}
</style>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head> <body>
<h1>Fetch json example</h1>
<ul>
</ul> </body>
<script>
var myList = document.querySelector('ul');
fetch('https://raw.githubusercontent.com/KylinBio-healthTechnology/lw/master/x.json')
.then(function(response) {
if (!response.ok) {
throw new Error("HTTP error, status = " + response.status);
}
return response.json();
})
.then(function(json) {
for(var i = 0; i < json.products.length; i++) {
var listItem = document.createElement('li');
listItem.innerHTML = '<strong>' + json.products[i].Name + '</strong>';
listItem.innerHTML +=' can be found in ' + json.products[i].Location + '.';
listItem.innerHTML +=' Cost: <strong>£' + json.products[i].Price + '</strong>';
myList.appendChild(listItem);
}
})
.catch(function(error) {
var p = document.createElement('p');
p.appendChild(
document.createTextNode('Error: ' + error.message)
);
document.body.insertBefore(p, myList);
});
</script>
</html>
json文件如下:https://raw.githubusercontent.com/KylinBio-healthTechnology/lw/master/x.json
{ "products" : [
{ "Name": "Cheese", "Price" : 2.50, "Location": "Refrigerated foods"},
{ "Name": "Crisps", "Price" : 3, "Location": "the Snack isle"},
{ "Name": "Pizza", "Price" : 4, "Location": "Refrigerated foods"},
{ "Name": "Chocolate", "Price" : 1.50, "Location": "the Snack isle"},
{ "Name": "Self-raising flour", "Price" : 1.50, "Location": "Home baking"},
{ "Name": "Ground almonds", "Price" : 3, "Location": "Home baking"}
]}
Using Fetch to rewrite JSON的更多相关文章
- Rewrite JSON with Fetch
1.重写json请求部分:HTML文件代码如下:<html>......<script> var myList = document.querySelector(‘ul‘); ...
- Rewrite JSON project with Fetch
上传 JSON 数据 使用fetch()来发布json编码的数据. var url = 'https://example.com/profile'; var data = {username: 'ex ...
- Rewrite json
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- express后端和fetch前端的json数据传递
在使用express做后端,前端使用fetch API来请求后端时,一般都是用 JSON 数据进行通信的. 下面是一个简单的例子: 前端: if (up) { var passwordAgain = ...
- react之fetch请求json数据
Fetch下载 npm install whatwg-fetch -S Fetch请求json数据 json文件要放在public内部才能被检索到
- react 项目实战(二)创建 用户添加 页面 及 fetch请求 json-server db.json -w -p 8000
1.安装 路由 npm install -S react-router@3.x 2.新增页面 我们现在的应用只有一个Hello React的页面,现在需要添加一个用于添加用户的页面. 首先在/src目 ...
- [Angular-Scaled Web] 8. Using $http to load JSON data
Using the $http service to make requests to remote servers. categories-model.js: angular.module('egg ...
- 你不需要jQuery(三):新AJAX方法fetch()
XMLHttpRequest来完成ajax有些老而过时了. fetch()能让我们完成类似 XMLHttpRequest (XHR) 提供的ajax功能.它们之间的主要区别是,Fetch API 使用 ...
- 腾讯IVWEB团队:前端 fetch 通信
欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:villainthr 文章摘自: 前端小吉米 随着前端异步的发展, XHR 这种耦合方式的书写不利于前端 ...
随机推荐
- Win32建立右键弹出菜单(PopMenu)
自定义右键菜单: #ifndef _CONTEXTMENU_H_ #define _CONTEXTMENU_H_ #include <windows.h> //动态菜单 #define I ...
- P3066 [USACO12DEC]逃跑的BarnRunning Away From
目录 题目 思路 错误&&注意 代码 题目 luoguP3066 思路 虽说这个题目有多种做法,但 左偏树算法: 我们发现这个合并的时候并不好合并,因为存的值不是固定的 那我们是不是可 ...
- 棋盘状态压缩dp
状态压缩入门DP整理 只针对入门 一般都是用2进制的方法,压缩成一个数,所以n的范围都会特变小 一些套路 状态一般是很多的,可以搜索或者位运算筛选一下,基本都是这样的吧 当要存两个状态或者数组存不下的 ...
- LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On
题目描述 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会.有 N×M 个这样 ...
- 断开网络连接的dos命令
https://zhidao.baidu.com/question/154994771.html 或者采用服务关闭开启模式,不过没有上面的好用net stop "network connec ...
- js中的&&和||
你是否看到过这样的代码:a=a||""; 可能javascript初学者会对此感到茫然.今天就跟大家分享一下我的一些心得. 其实: a=a||"defaultValue& ...
- POJ 3162 Walking Race(树形dp+单调队列 or 线段树)
http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...
- 读书笔记:Spring boot实战
第一章 入门 Spring boot最重要的四个核心 : 1.自动配置:针对很多spring应用程序常见的应用功能,spring boot能自动提供相关配置 2.起步依赖:告诉spring boot需 ...
- c++ 匹配A容器中最先出现的b容器中的元素,返回iterator,(find_first_of)
#include <iostream> // std::cout #include <algorithm> // std::find_first_of #include < ...
- html 获取鼠标左键事件,滚轮点击事件,右键点击事件
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...