如何在另一个函数中使用一个JavaScript函数接收的JSON对象?

时间:2022-07-21 16:54:55

I had written a function which returns JSON format via ajax call. The JSON is

我编写了一个函数,通过ajax调用返回JSON格式。 JSON是

 {
    "communication": [{
        "communication_name": "None",
        "communication_id": "1"
    }],
    "hardware": [{
        "hardware_name": "XXXXXXXX",
        "hardware_id": "6"
    }],
    "Sofware": [{
        "software_name": "XXXXXX",
        "software_id": "3"
    }, {
        "software_name": "XXXXXXXXXXXXX",
        "software_id": "4"
    }]
}

And this is the JavaScript function for getting this response:

这是获取此响应的JavaScript函数:

function getModelData(model_id, model_name){

     var xmlHttp = createXmlHttpRequestObject();

    try {
    xmlHttp.open("GET", "ajaxmodel_new.php?model_id="+model_id,true);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status==200){
        var myJSONObject =  JSON.parse(xmlHttp.responseText)
        //alert(myJSONObject.communication[0].communication_name)
         }
    }
    xmlHttp.send(null);

    } catch (e){
    alert("Can't connect to server:\n" + e.toString());
    }
}

It is getting the correct response. And there is another function is for getting the selected value in the selected drop down:

它得到了正确的回应。还有另一个功能是获取所选下拉列表中的选定值:

<div id="selected_options">
                <select onchange="test()" id="selected_opt">
                <option value="0" selected>-Select-</option>
                <option value="1">Communication</option>
                             </select></div>
function test() {
    var get_id = document.getElementById('selected_opt');
    var result = get_id.options[get_id.selectedIndex].value;
    alert(result);
}

Objective

I need to use the JSON response, i.e., myJSONObject, in the test() function. How can I use that var myJSONObject which is obtained in getModelData() ajax function in test() function?

我需要在test()函数中使用JSON响应,即myJSONObject。我怎样才能使用在test()函数中的getModelData()ajax函数中获得的var myJSONObject?

2 个解决方案

#1


4  

I had a different approach in parsing the JSON Object.

我在解析JSON对象时有不同的方法。

From the PHP side I create the object and store it in a variable say, $JSONvar. Now I echo the variable.

从PHP方面我创建了对象并将其存储在变量中,例如$ JSONvar。现在我回应变量。

Then on the client side this is how I intercept the object.

然后在客户端这就是我拦截对象的方式。

var JsonObject = {};
var http = new XMLHttpRequest();
http.open("GET", url, true); //url is the url echoing the jsonString
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200) {
    var responseTxt = http.responseText;
    myJSONObject = eval('(' + responseTxt + ')');
    test(myJSONObject);
 }
}
http.send(null);

Your test function should be able to access the object like this,

您的测试函数应该能够像这样访问对象,

function test(myJSONObject) {
var object = myJSONObject;
alert(object);
var get_id = document.getElementById('selected_opt');
var result = get_id.options[get_id.selectedIndex].value;
alert(result);
}

#2


2  

Call test() from the function in xmlHttp.onreadystatechange, passing the decoded object as a parameter.

从xmlHttp.onreadystatechange中的函数调用test(),将解码的对象作为参数传递。

#1


4  

I had a different approach in parsing the JSON Object.

我在解析JSON对象时有不同的方法。

From the PHP side I create the object and store it in a variable say, $JSONvar. Now I echo the variable.

从PHP方面我创建了对象并将其存储在变量中,例如$ JSONvar。现在我回应变量。

Then on the client side this is how I intercept the object.

然后在客户端这就是我拦截对象的方式。

var JsonObject = {};
var http = new XMLHttpRequest();
http.open("GET", url, true); //url is the url echoing the jsonString
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200) {
    var responseTxt = http.responseText;
    myJSONObject = eval('(' + responseTxt + ')');
    test(myJSONObject);
 }
}
http.send(null);

Your test function should be able to access the object like this,

您的测试函数应该能够像这样访问对象,

function test(myJSONObject) {
var object = myJSONObject;
alert(object);
var get_id = document.getElementById('selected_opt');
var result = get_id.options[get_id.selectedIndex].value;
alert(result);
}

#2


2  

Call test() from the function in xmlHttp.onreadystatechange, passing the decoded object as a parameter.

从xmlHttp.onreadystatechange中的函数调用test(),将解码的对象作为参数传递。