就像在標題上顯示的一樣,我想用相對路徑方法讀取jquery中的json文件。首先,我是這個工作的開端,這可能是我嘗試的蠢事。用jquery讀取外部json文件的相對路徑方法
我有一個外部的json文件,我想在我的腳本中打開這個文件。由於json文件必須具有外部的,開發後會因某種原因而改變,並且會根據過程經常變化。
這裏是我的json文件。
{
"revisiondate": "21 April 2016",
"documentname": "1658MC",
"department": "Sales",
"description": "Available",
"link": "href=1658MC.pdf"
}, {
"revisiondate": "16 April 2016",
"documentname": "VCX16B",
"department": "Enginnering",
"description": "Not Available",
"link": "href=VCX16B.pdf"
}, {
"revisiondate": "15 March 2016",
"documentname": "AB36F",
"department": "Custumer Services",
"description": "Not Available",
"link": "href=AB36F.pdf"
}, {
"revisiondate": "12 Agust 2016",
"documentname": "FC25D",
"department": "Technical Support",
"description": "Not Available",
"link": "href=FC25D.pdf"
}
而這個JSON文件保存到我的本地文件
C:.......\Desktop\Deneme16\Deneme161.json
要讀取該文件與我的腳本
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<table id="userdata" border="0.02">
<th>Revision Date</th>
<th>Document Name</th>
<th>Department </th>
<th>Description</th>
<th>Link</th>
</table>
<script>
$.ready(function() {
myObjects = {};
$.getJSON('deneme16.json', function(data) {
myObjects.myJson = data;
});
$.each(data.person, function(i, person) {
var tblRow = "<tr><td>" + person.revisiondate +
"</td><td>" + person.documentname +
"</td><td>" + person.department +
"</td><td>" + person.description +
"</td><td><a target='_blank' href='"
+ person.link.split('href=')[1]
+"' >"+person.link.split('href=')[1]
+"</a></td></tr>"
$(tblRow).appendTo("#userdata tbody");
});
});
</script>
</body>
</html>
我怎麼能讀與jQuery這個本地文件?我讀了很多標題,但有些標題在討論ajax.request,有些人在談論parse.jquery,有些人則因爲安全策略而對它進行談論是不可能的。我嘗試了很多東西,最後我決定編寫這些代碼。我很困惑,我想解決這個問題。有人可以解釋問題在哪裏oparete此代碼
有你使用getJSON,console.log(data)獲取數據? –