当我从服务器加载json文件时,我遇到了一个小问题,我使用的代码是:
function dataLoad(){
//console.log('loading json...');
var pathUrl = "public_html/wp-content/themes/turisti/include/main.json";
var places = [];
$.ajax({
url: pathUrl,
dataType: 'jsonp',
timeout: 5000,
success: function(data, status) {
console.log("success");
},
error: function() {
console.log("some errors");
}
});
}
我在DOM的ready事件上调用此函数,遗憾的是我可以加载文件,路径是正确的我通过ssh终端验证,但函数调用错误回调,有人有一些想法来解决这个问题?
答案 0 :(得分:0)
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
我使用上面的json文件作为示例,您应该能够在代码下面读取json文件;
function getJsonData() {
console.log("getJsonData is running...");
$.ajax({
url: "/_assets/js/json1.json",
dataType: "text",
success: function (data) {
var json = $.parseJSON(data);
//we have json object here
//then you can reach like below
console.log("object : " + json.menu);
console.log("objects' item : " + json.menu.id);
}
});
}