我有一个接受JSON对象的简单变量......
以下是所有代码:
这应该有所帮助,因为我认为它足够干净,不会露出我正在工作的地方。
var server = "00.00.000.00:8800";
var webServices = "http://" + server + "/somedir/ws/";
var networksURL;
var sitesURL;
var resourcesURL;
var componentsURL;
networksURL = webServices + 'config/networks';
sitesURL = webServices + 'config/sites';
resourcesURL = webServices + 'config/resources';
componentsURL = webServices + 'config/components';
var treeData;
var netsSites;
var sitesRes;
var resComp;
var myType;
var myUrl;
var jsonLen;
var myData1;
var JSONModified;
var dataToget;
function getInfo(myUrl, callback) {
console.log("INSIDE GETINFO:: " + myUrl);
var myData;
$.ajax({
type: 'GET',
url: myUrl,
async: true,
crossDomain: true,
//data: "{}", //NOT USED unless you have parameters
//jsonpCallback: 'jsonpCallback', //NOT USED unless the server is set up properly
dataType: 'json',
//Switch to jsonp ONLY if your JSON CALLBACK is set up properly on the server
contentType: 'application/json',
success: function(data) {
jsonLen = data.length;
myData1 = data;
//If successful, show the data, if not, show error
if (!data) {
console.log("Error");
//$("#alert_json").text("There was an error processing the your request.");
return false;
} else {
myData = JSON.stringify(data);
//**************************** STEP 1 ***************************
//Now send to processing (STEP 1) add the prefixes for the JSTree
JSONModified = addPrefix(myData);
//console.log(JSONModified);
//End sending back the data
//**************************** END STEP 1 ***********************
//console.log("Data found: " + myData);
//$("#alert_json").text(JSONModified);
if (JSONModified)
{
callback(JSONModified);
} else {
callback('[{"data":"No site data found..."}]');
}
}
},
error: function(data) {
//console.log("Data not found or is not available at this time");
//$("#alert_json").text("Data not found or is not available at this time");
callback(null);
}
});
}
//PRELIMINARY - CALL THE FUNCTION passing in the JSON object you wish to retreive
function buildTree(typJSON) {
myType = typJSON;
//DETERMINE The type of JSON and then set it below.
switch (typJSON) {
case "nets":
dataToget = networksURL;
break;
case "sties":
dataToget = sitesURL;
break;
case "res":
dataToget = resourcesURL;
break;
case "comp":
dataToget = componentsURL;
break;
default:
dataToget = 'Unknown Error';
break;
}
getInfo(dataToget, function(data) {
if (!data) {
console.log("Return Value: NULL");
return false;
} else {
//console.log("Return Value: SUCCESS!");
//console.log(JSONModified);
// FINAL STEP!
switch (myType)
{
case "nets":
treeData = JSONModified;
return treeData; //SEND BACK THE JSON to the
break;
case "sites":
netsSites = JSONModified;
return netsSites; //SEND BACK THE JSON to the
break;
case "res":
sitesRes = JSONModified;
return sitesRes; //SEND BACK THE JSON to the
break;
case "comp":
resComp = JSONModified;
return resComp; //SEND BACK THE JSON to the
break;
default:
return '[{"data":"No tree data found..."}]';
}
//treeviewDetails.html to build the tree ONLY if called from tree
}
});
}
//**************************** STEP 1 ***************************
//Now process the JSON object and get the names of all the groups; albeit networks, sites, resources, components
function addPrefix(rawJSON) {
var jsonArray = JSON.parse(rawJSON); // parse the JSON string to an actual array
//Loop through all the groups of .... networks, sites, resources, components
$.each(jsonArray, function(index, element) {
element.data = element.name; //This can be INDEX or any other part...
element.metadata = {
id: element.id,
note: element.notes
};
netsSites = _mySites(element.id);
console.log("This is a site: " + netsSites);
//If eval(getSites(element.id)) then make children
if(netsSites !== "[]" || netsSites !== "")
{
element.children = eval(netsSites);
}
});
console.log("Ret from GetInfo: " + JSON.stringify(jsonArray));
return JSON.stringify(jsonArray); // converts the array, with the new properties, back to a JSON string
}
//ERROR HANDLING
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
}
console.Log('Error: ' + msg);
}
//In the basement, we're going to grab the sites, resources, and components
//With Callback...
function _mySites(netID) {
myNetwork = networksURL + "/" + netID + "/sites";
console.log(myNetwork);
return getInfo(myNetwork, function(data){});
}
更新...查看上面的所有代码......这应该有所帮助。
感谢。
答案 0 :(得分:0)
也许这是拼写错误的?
case "sties":
答案 1 :(得分:0)
你的问题在这里:
return getInfo(myNetwork, function(data){});
getInfo
不会返回任何内容。它调用异步的AJAX请求。因此,_mySites
返回undefined,因为这是getInfo
返回的内容。
你需要在这里使用getInfo
的回调。
function _mySites(netID, callback) {
myNetwork = networksURL + "/" + netID + "/sites";
console.log(myNetwork);
getInfo(myNetwork, callback);
}
然后做:
_mySites(element.id, function(netsSites){
console.log("This is a site: " + netsSites);
});
这意味着您的addPrefix
无法返回任何内容,因为它是异步的。您可能需要在此重新考虑整个结构。