After creating a function that returns JSON format through an ajax call, I received the following JSON data:
{
"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"
}]
}
The JavaScript function used to retrieve this response is as follows:
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());
}
}
The correct response is being obtained. Another function is created to get the selected value from a dropdown menu:
<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
The goal now is to utilize the JSON response, specifically myJSONObject
, in the test()
function. How can the variable myJSONObject
obtained in the getModelData()
ajax function be utilized in the test()
function?