当前位置: 首页 > news >正文

Vue使用fetch获取本地数据

(1)使用get

test.json文件

{

    "list":[111,222,333]

}

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body><div id="box"><button @click="handleClick">click</button>{{list}}</div><script>var obj = {data(){return{list:[]}},methods: {handleClick(){// 基于promisefetch("./test.json").then(// res=>{return res.json()}res=>res.json() // 简写 返回请求头响应头的一些信息).then(res=>{console.log(res,'res')this.list = res.list})// fetch("./test.json")// .then(//     res=>res.text() // 不确定是json格式 用text保险// )// .then(res=>{//     console.log(res,'res')// })}},}Vue.createApp(obj).mount('#box')</script>
</body>
</html>

遇到报错如下

安装插件 Preview on Web Server  (使用Open with Live Server会有点小bug)

(2)使用post

1)安装工具 帮助创建接口 npm i json-server -g 允许跨域

2)启动命令 使用json-server --watch .\test2.json 访问文件;中间报错是json文件内容格式不对,修改一下

3)访问http://localhost:3000/list ;如果遇到禁止运行脚本就用管理员身份设置 set-ExecutionPolicy RemoteSigned

4)代码部分

test2.json文件

{

  "list": [

    {

      "name": "seki",

      "age": 18,

      "id": "1"

    }

  ]

}

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body><div id="box"><button @click="handleClick">click</button>{{list}}</div><script>var obj = {data(){return{list:null}},methods: {handleClick(){// 基于promise// fetch("http://localhost:3000/list")// .then(res=>res.json())// .then(res=>{//     console.log(res,'res')//     this.list = res// })// 方式2var params ={name:'cj',age:20}fetch("http://localhost:3000/list",{method:"POST", // post POST 都可以 headers:{'Content-Type':'application/json', // 不同请求头入参格式不一样},body:JSON.stringify(params)}).then(res=>res.json()).then(res=>{console.log(res)})// put 更新// fetch("http://localhost:3000/list/2",{//     method:"put", //     headers:{//         'Content-Type':'application/json',//     },//     body:JSON.stringify({name:'xiaoxin'})// }).then(res=>res.json())// .then(res=>{//     console.log(res)// })// fetch("http://localhost:3000/list/2",{//     method:"delete", //     headers:{//         'Content-Type':'application/json',//     },// }).then(res=>res.json())// .then(res=>{//     console.log(res)// })}},}Vue.createApp(obj).mount('#box')</script>
</body>
</html>

http://www.lryc.cn/news/433928.html

相关文章:

  • 《酒饮真经》秘籍4,让你的酒场技巧更上一层楼!
  • 回车符与快捷键记录
  • 计算机网络-VRRP工作原理
  • 6.5椒盐噪声
  • CSS样式的引用方式以及选择器使用
  • Python Flask_APScheduler定时任务的正确(最佳)使用
  • Linux命名管道
  • Xinstall助力App全渠道统计,参数传递下载提升用户体验!
  • 【时时三省】(C语言基础)指针进阶 例题4
  • k8s的配置管理
  • JAVA- 多线程
  • 【Qt】解决设置QPlainTextEdit控件的Tab为4个空格
  • elementUI根据列表id进行列合并@莫成尘
  • 基于人工智能的智能安防监控系统
  • 分享从零开始学习网络设备配置--任务6.3 使用基本ACL限制网络访问
  • 数据结构——线性表(静态链表、循环链表以及双向链表)
  • vue3_对接腾讯_实时音视频
  • 一台电脑对应一个IP地址吗?‌探讨两台电脑共用IP的可能性
  • XInput手柄输入封装
  • NodeMCU-ESP8266+flash_download_tool_3.9.7 烧录
  • 首例开源的自动驾驶混合运动规划框架,手握“规划可解释”和“决策准确”两张王牌!
  • 数据结构之红黑树的 “奥秘“
  • 【鸿蒙 HarmonyOS NEXT】使用EventHub进行数据通信
  • 大模型RAG实战|构建知识库:文档和网页的加载、转换、索引与存储
  • 江协科技stm32————11-5 硬件SPI读写W25Q64
  • 网络编程day04(UDP、Linux IO 模型)
  • 【android10】【binder】【2.servicemanager启动——全源码分析】
  • Java实现简易计算器功能(idea)
  • Parsec问题解决方案
  • Swift 创建扩展(Extension)