步骤 2 : 准备 json数据 步骤 3 : 通过 fetch 获取数据
本来要开始讲解 Vue.js 里如何使用 Ajax了。 一般说来 Vue 不会直接使用原生的 Ajax 而是使用 ajax 框架。 而 fetch.js 就是眼下比较流行的一种 ajax 框架
json 数据很简单,然后我把他放在:http://how2j.cn/study/json.txt 方便调用
{"name":"gareen","hp":"355"}
{"name":"gareen","hp":"355"}
如果要本地测试,可以在右上角下载 fetch.min.js
<script src="https://how2j.cn/study/fetch.min.js"></script>
<div id="hero">
</div>
<script>
var url = "http://how2j.cn/study/json.txt"
fetch(url).then(function(response) {
response.json().then(function (jsonObject) {
var jsonString = JSON.stringify(jsonObject)
document.getElementById("hero").innerHTML = "通过fetch获取到的json数据:"+ jsonString;
})
}).catch(function(err){
console.log("Fetch错误:"+err);
})
</script>
<script src="https://how2j.cn/study/fetch.min.js"></script> <div id="hero"> </div> <script> var url = "http://how2j.cn/study/json.txt" fetch(url).then(function(response) { response.json().then(function (jsonObject) { var jsonString = JSON.stringify(jsonObject) document.getElementById("hero").innerHTML = "通过fetch获取到的json数据:"+ jsonString; }) }).catch(function(err){ console.log("Fetch错误:"+err); }) </script>
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|