how2j.cn

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

步骤 1 :

axios.js

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

准备 json数据

json 数据很简单,然后我把他放在:http://how2j.cn/study/json.txt 方便调用
"); window.frames["iframe_show7836"].document.write(decodeHtml($("textarea#stepcodeTextarea7836").val())); window.frames["iframe_show7836"].document.close(); $(window.frames["iframe_show7836"]).load(function(){ $("#iframe_show7836").height($("#iframe_show7836").contents().find("body").height()+showittryitheight); }); $("#iframe_show7836").height($("#iframe_show7836").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
{"name":"gareen","hp":"355"}
{"name":"gareen","hp":"355"}
"); window.frames["iframe7836"].document.write(decodeHtml(code7836)); window.frames["iframe7836"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7836"]).load(function(){ $("#iframe7836").height($("#iframe7836").contents().find("body").height()+showittryitheight); }); $("#iframe7836").height($("#iframe7836").contents().find("body").height()+showittryitheight); alreadyWriteCode7836 = code7836; $("#rendering7836").hide(); $("#rendered7836").show(); } var tRereshRetry2DemoPanel7836 = setInterval(rereshRetry2DemoPanel7836,1000); var binded7836 = false; $("textarea#stepcodeTextarea7836").keyup(function(){ if(!binded7836){ $(window).bind('beforeunload',function(){ binded7836 = true; return "xxxx"; }); } var newCode = $(this).val() code7836 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7836!=newCode){ // window.frames["iframe7836"].document.write("
"); // window.frames["iframe7836"].document.write(decodeHtml($("textarea#stepcodeTextarea7836").val())); // window.frames["iframe7836"].document.close(); // $(window.frames["iframe7836"]).load(function(){ // $("#iframe7836").height($("#iframe7836").contents().find("body").height()+showittryitheight); // }); // code7836 = newCode; // } }); $(".tryButton7836").click(function(){ $("#tryDiv7836").show(); $("#stepcodeTextarea7836").focus(); $("#stepcodeTextarea7836").height(200); $("#iframe7836").height(0); window.frames["iframe7836"].document.write("
"); window.frames["iframe7836"].document.write(decodeHtml($("textarea#stepcodeTextarea7836").val())); window.frames["iframe7836"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7836"]).load(function(){ $("#iframe7836").height($("#iframe7836").contents().find("body").height()+showittryitheight); }); $("#iframe7836").height($("#iframe7836").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7836.focus(); editor7836.setSize(null, "250"); $("#rendering7836").hide(); $("#rendered7836").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 editor7836 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7836"), { 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); } } }); editor7836.on("change",function(doc){ if(!binded7836){ $(window).bind('beforeunload',function(){ binded7836 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7836 = newCode; $("textarea#stepcodeTextarea7836").val(newCode); if(alreadyWriteCode7836!=code7836){ lastModifedTime7836 = new Date().getTime(); $("#rendering7836").show(); $("#rendered7836").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7836 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7836.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7836").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

通过 axions 获取数据

如果要本地测试,可以在右上角下载 axios.min.js
"); window.frames["iframe_show7837"].document.write(decodeHtml($("textarea#stepcodeTextarea7837").val())); window.frames["iframe_show7837"].document.close(); $(window.frames["iframe_show7837"]).load(function(){ $("#iframe_show7837").height($("#iframe_show7837").contents().find("body").height()+showittryitheight); }); $("#iframe_show7837").height($("#iframe_show7837").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/axios.min.js"></script> <div id="hero"> </div> <script> var url = "http://how2j.cn/study/json.txt" axios.get(url).then(function(response) { var jsonObject = response.data; var jsonString = JSON.stringify(jsonObject) document.getElementById("hero").innerHTML = "通过 axios 获取到的json数据:"+ jsonString; }) </script>
<script src="http://127.0.0.1/study/axios.min.js"></script>
 
<div id="hero">
 
</div>
 
<script>
var url = "http://how2j.cn/study/json.txt"
	axios.get(url).then(function(response) {
		var jsonObject = response.data;
		var jsonString = JSON.stringify(jsonObject)
		document.getElementById("hero").innerHTML = "通过 axios 获取到的json数据:"+ jsonString;
	})
 
</script>
"); window.frames["iframe7837"].document.write(decodeHtml(code7837)); window.frames["iframe7837"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7837"]).load(function(){ $("#iframe7837").height($("#iframe7837").contents().find("body").height()+showittryitheight); }); $("#iframe7837").height($("#iframe7837").contents().find("body").height()+showittryitheight); alreadyWriteCode7837 = code7837; $("#rendering7837").hide(); $("#rendered7837").show(); } var tRereshRetry2DemoPanel7837 = setInterval(rereshRetry2DemoPanel7837,1000); var binded7837 = false; $("textarea#stepcodeTextarea7837").keyup(function(){ if(!binded7837){ $(window).bind('beforeunload',function(){ binded7837 = true; return "xxxx"; }); } var newCode = $(this).val() code7837 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7837!=newCode){ // window.frames["iframe7837"].document.write("
"); // window.frames["iframe7837"].document.write(decodeHtml($("textarea#stepcodeTextarea7837").val())); // window.frames["iframe7837"].document.close(); // $(window.frames["iframe7837"]).load(function(){ // $("#iframe7837").height($("#iframe7837").contents().find("body").height()+showittryitheight); // }); // code7837 = newCode; // } }); $(".tryButton7837").click(function(){ $("#tryDiv7837").show(); $("#stepcodeTextarea7837").focus(); $("#stepcodeTextarea7837").height(200); $("#iframe7837").height(0); window.frames["iframe7837"].document.write("
"); window.frames["iframe7837"].document.write(decodeHtml($("textarea#stepcodeTextarea7837").val())); window.frames["iframe7837"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7837"]).load(function(){ $("#iframe7837").height($("#iframe7837").contents().find("body").height()+showittryitheight); }); $("#iframe7837").height($("#iframe7837").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7837.focus(); editor7837.setSize(null, "250"); $("#rendering7837").hide(); $("#rendered7837").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 editor7837 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7837"), { 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); } } }); editor7837.on("change",function(doc){ if(!binded7837){ $(window).bind('beforeunload',function(){ binded7837 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7837 = newCode; $("textarea#stepcodeTextarea7837").val(newCode); if(alreadyWriteCode7837!=code7837){ lastModifedTime7837 = new Date().getTime(); $("#rendering7837").show(); $("#rendered7837").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7837 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7837.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7837").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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


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


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

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

上传截图