how2j.cn

相关下载
文件名 文件大小
fetch.min.js 13k
步骤 1 : fetch.js   
步骤 2 : 准备 json数据   
步骤 3 : 通过 fetch 获取数据   

步骤 1 :

fetch.js

本来要开始讲解 Vue.js 里如何使用 Ajax了。 一般说来 Vue 不会直接使用原生的 Ajax 而是使用 ajax 框架。 而 fetch.js 就是眼下比较流行的一种 ajax 框架
步骤 2 :

准备 json数据

json 数据很简单,然后我把他放在:http://how2j.cn/study/json.txt 方便调用
{"name":"gareen","hp":"355"}
{"name":"gareen","hp":"355"}
步骤 3 :

通过 fetch 获取数据

如果要本地测试,可以在右上角下载 fetch.min.js
"); window.frames["iframe_show7834"].document.write(decodeHtml($("textarea#stepcodeTextarea7834").val())); window.frames["iframe_show7834"].document.close(); $(window.frames["iframe_show7834"]).load(function(){ $("#iframe_show7834").height($("#iframe_show7834").contents().find("body").height()+showittryitheight); }); $("#iframe_show7834").height($("#iframe_show7834").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/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="http://127.0.0.1/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>
"); window.frames["iframe7834"].document.write(decodeHtml(code7834)); window.frames["iframe7834"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7834"]).load(function(){ $("#iframe7834").height($("#iframe7834").contents().find("body").height()+showittryitheight); }); $("#iframe7834").height($("#iframe7834").contents().find("body").height()+showittryitheight); alreadyWriteCode7834 = code7834; $("#rendering7834").hide(); $("#rendered7834").show(); } var tRereshRetry2DemoPanel7834 = setInterval(rereshRetry2DemoPanel7834,1000); var binded7834 = false; $("textarea#stepcodeTextarea7834").keyup(function(){ if(!binded7834){ $(window).bind('beforeunload',function(){ binded7834 = true; return "xxxx"; }); } var newCode = $(this).val() code7834 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7834!=newCode){ // window.frames["iframe7834"].document.write("
"); // window.frames["iframe7834"].document.write(decodeHtml($("textarea#stepcodeTextarea7834").val())); // window.frames["iframe7834"].document.close(); // $(window.frames["iframe7834"]).load(function(){ // $("#iframe7834").height($("#iframe7834").contents().find("body").height()+showittryitheight); // }); // code7834 = newCode; // } }); $(".tryButton7834").click(function(){ $("#tryDiv7834").show(); $("#stepcodeTextarea7834").focus(); $("#stepcodeTextarea7834").height(200); $("#iframe7834").height(0); window.frames["iframe7834"].document.write("
"); window.frames["iframe7834"].document.write(decodeHtml($("textarea#stepcodeTextarea7834").val())); window.frames["iframe7834"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7834"]).load(function(){ $("#iframe7834").height($("#iframe7834").contents().find("body").height()+showittryitheight); }); $("#iframe7834").height($("#iframe7834").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7834.focus(); editor7834.setSize(null, "250"); $("#rendering7834").hide(); $("#rendered7834").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7834 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7834"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7834.on("change",function(doc){ if(!binded7834){ $(window).bind('beforeunload',function(){ binded7834 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7834 = newCode; $("textarea#stepcodeTextarea7834").val(newCode); if(alreadyWriteCode7834!=code7834){ lastModifedTime7834 = new Date().getTime(); $("#rendering7834").show(); $("#rendered7834").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7834 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7834.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7834").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-Vue.js-fetch.js 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图