1.首先應當明確像這樣的目錄結構:
存儲json字符串的lessonform.txt文件就是模擬存儲服務器上的數據。由於我自己代碼中ajax請求的url是
因此lessonform.txt文件應該與我的工程位於同一目錄下,內容如下:
直接上代碼:
//get the form of course
window.onload = function() {
ajax('lessonform.txt', function (res) {
var json = JSON.parse(res);
for (var attr in json) {
var html = '<tr><td><input type="checkbox" name="part"></td>';
for (var attr1 in json[attr]) {
html += '<td>' + json[attr][attr1] + '</td>';
//console.log(json[attr][attr1]);
}
html += '<td><i class="fa fa-trash-o cur-p delete"></i></td></tr>';
$('.table-information').children().eq(0).append(html);
}
PrintBackColor();
deleteoneElement();
benchdelete();
})
}
滿心歡喜地刷新頁面,結果發現這樣的事情:
啊偶·····亂碼了
就想着修改一下文本文件的編碼格式。打開lessonform.txt,點擊文件-》另存爲,會發現下面有一個選擇編碼格式的地方,一般.txt都默認這個格式:
試着把它改成和html文件一樣的編碼格式:
保存之後重新刷新頁面:
okay,就醬紫~