<style>
#btn{
width: 50px;
height: 50px;
background-color: red;
}
#output{
width: 100px;
height: 100px;
background-color: pink;
}
</style> <button id="btn"></button>
<div id="output"></div>
  //jsong格式数据
          [
            {
              "name":"张三",
              "age":25
            },
            {
              "name":"李四",
              "age":28
            }
          ]
  //js代码
    <script>
btn.onclick=function(){
fetch("data.json",{
headers:{
"Content-Type":"application/json"
},
}).then(function(res){
return res.json();
}).then(function(data){
console.log(data);
var str="";
data.forEach(item => {
str+=`<p>${item.name}<p>` });
document.getElementById("output").innerHTML=str; }).catch(error=>console.log(error))
}
</script>
/* Fetch发送请求 get*/
var page=1;
var pageSize=5;
var totalPage=0;
fetch(`/user/queryUser?page=${page}&pageSize=${pageSize}`, { headers: {
  'Content-Type': 'application/json; charset=UTF-8'
}, }).then(res => res.json()).then(res => {
  console.log(res);
  totalPage=(Math.ceil(res.total/pageSize));
  var html = template("userTpl", res);
  console.log(html);
  $("#userBox").html(html);
}).catch(err => {
console.log(err);
})
//Fetch请求 post
var data={
id:id,
isDelete:isDelete
}
fetch("/user/updateUser",{
headers: {
    'Content-Type': 'application/json; charset=UTF-8'
},
    method:"post",
    body:JSON.stringify(data),
}).then(res=>res.json()).then(res=>{
   console.log(res);
  if(res.success){
    location.reload();
}else{
  if(res.error){
    alert(res.message);
}
}
})
 

Fetch请求后台的数据的更多相关文章

  1. 一、表单和ajax中的post请求&&后台获取数据方法

    一.表单和ajax中的post请求&&后台获取数据方法 最近要做后台数据接收,因为前台传来的数据太过于混乱,所以总结了一下前台数据post请求方法,顺便写了下相对应的后台接收方法. 前 ...

  2. 【11】ajax请求后台接口数据与返回值处理js写法

    $.ajax({ url: "/test.php",//后台提供的接口 type: "post",   //请求方式是post data:{"type ...

  3. AFN请求后台返回数据为NSInlineData类型的处理

    在利用AFN进行数据解析时出现返回数据为 <7b227374 61747573 223a302c 226d6573 73616765 223a22e6 82a8e79a 84e6898b e69 ...

  4. echarts 中 请求后台改变数据

    function tablenumber() { $.ajax({ type : "get", url : "../res/error.json", dataT ...

  5. node后台fetch请求数据-Hostname/IP doesn't match certificate's altnames解决方法

    一.问题背景 基于express框架,node后台fetch请求数据,报错Hostname/IP doesn't match certificate's altnames..... require(' ...

  6. Swift - 后台获取数据(Background Fetch)的实现

    前面讲了如何让程序申请后台短时运行.但这个额外延长的时间毕竟有限.所以从iOS7起又引入两种在后台运行任务的方式:后台获取和后台通知. 1,后台获取介绍 后台获取(Background Fetch)是 ...

  7. 前后端数据交互(四)——fetch 请求详解

    fetch 是 XMLHttpRequest 的升级版,使用js脚本发出网络请求,但是与 XMLHttpRequest 不同的是,fetch 方式使用 Promise,相比 XMLHttpReques ...

  8. js页面用定时任务通过AJAX获取后台数据,但是从这个页面跳转到其他页面后,定时任务仍然在定时请求后台

    setInterval(function(){//ajax 请求后台数据},1000);这个是A页面的定时器然后我在A页面通过其他请求跳转到其他页面之后后台发现A页面的定时器的那个请求仍然在执行为什么 ...

  9. React native 中使用Fetch请求数据

    一.代码 import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from ' ...

随机推荐

  1. Spring Cloud Config采用数据库存储配置内容

    在之前的<Spring Cloud构建微服务架构:分布式配置中心>一文中,我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储.这一设计巧妙的利用Gi ...

  2. spring-cloud-starter-gateway

    ********************************************************** Spring MVC found on classpath, which is i ...

  3. [转]bitcoin: 通过 rpc 请求节点数据

    本文转自:https://laravel-china.org/index.php/articles/8919/bitcoin-requests-node-data-through-rpc 文章来自本人 ...

  4. 【转载】Windows Server2012安装IIS服务器

    在云服务器的使用过程中,很多人由于习惯或者实际需要,会选择Windows Server系统服务器,较常用的版本有Windows Server2008.Windows Server2012.在Windo ...

  5. linux下定时执行任务的方法

    linux下定时执行任务的方法 在LINUX中你应该先输入crontab -e,然后就会有个vi编辑界面,再输入0 3 * * 1 /clearigame2内容到里面 :wq 保存退出. 在LINUX ...

  6. SVN服务器本地搭建与使用

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6623026.html  使用SVN进行代码管理,需要搭建服务端资源库,然后在使用SVN的电脑安装SVN客户端或 ...

  7. Gvim 和vim 有什么区别

    Gvim 和vim 有什么区别 Gvim是windows的 vim是linux的黑色的命令符 Gvim是单独的窗口下的vim,像notepad一样. vim就是在黑乎乎的cmd窗口下的编辑器.wind ...

  8. Python全栈开发之---输入输出与流程控制

    Python简介 python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白.大家都知道,当下全栈工程师的概念很火 ...

  9. 12个必备的JavaScript装逼技巧

    译者按: 无论你是初学者还是资深人士,都值得一读! 原文: 12 Amazing JavaScript Shorthand Techniques 译者: Fundebug 为了保证可读性,本文采用意译 ...

  10. spring boot之hello

    自己使用springboot也已经写过一段时间的代码,但是对springboot真正运行的流程还是有点模糊,今天写出自己对springboot的认识,如有不对,还请各位大佬不吝赐教,话不多说,直接上代 ...