|
步骤
1
:
axios.js
步骤
2
:
准备 json数据
步骤
3
:
通过 axions 获取数据
本来要开始讲解 Vue.js 里如何使用 Ajax了。 一般说来 Vue 不会直接使用原生的 Ajax 而是使用 ajax 框架。 而 axios.js 就是眼下比较流行的一种 ajax 框架
");
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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|