Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当一个JS变量不能很好地运行并且无法填充时_Javascript_Json - Fatal编程技术网

Javascript 当一个JS变量不能很好地运行并且无法填充时

Javascript 当一个JS变量不能很好地运行并且无法填充时,javascript,json,Javascript,Json,我有一个简单的变量,它接受一个JSON对象 以下是所有代码: 这应该会有帮助,因为我认为它足够干净,不会暴露我工作的地方 var server = "00.00.000.00:8800"; var webServices = "http://" + server + "/somedir/ws/"; var networksURL; var sitesURL; var resourcesURL; var componentsURL; networksURL = webServices + 'c

我有一个简单的变量,它接受一个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){});
    }
更新。。。请参阅上面的所有代码。。。这应该会有所帮助


谢谢。

也许是拼错了

case "sties":
你的问题是:

return getInfo(myNetwork, function(data){});
getInfo
不返回任何内容。它调用一个异步的AJAX请求。因此,
\u 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
无法返回任何内容,因为它是异步的。您可能需要在这里重新考虑您的整个结构。

可能您设置为
mydata
的JSON无效?什么是
getInfo
?在哪里定义了
\u mySites
?是否在同一范围内?我这样问是因为你要把函数赋给一个局部变量。什么是
getInfo
?这是AJAX调用吗?因为AJAX是异步的!你不能从AJAX调用返回,你可能需要使用回调(
function(data){}
)。为什么
getSites
有一个
mydata
参数?@Peter:如果
getInfo
运行AJAX调用,那么它就不能返回任何东西!你需要在回调中执行你的
$。每个(jsonArray
。谢谢…哦!又一个荷马·辛普森时刻