如何在另一个函数中使用一个 JavaScript 函数接收到的 JSON 对象?
我编写了一个通过 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"
}]
}
这是用于获取此响应的 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());
}
}
它正在获取正确的响应。还有另一个函数用于获取所选下拉列表中的所选值:
<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);
}
目标
我需要在 test()
函数中使用 JSON 响应,即 myJSONObject
。如何在 test()
函数中使用在 getModelData()
ajax 函数中获得的 var myJSONObject
?
I had written a function which returns JSON format via ajax call. The JSON is
{
"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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在解析 JSON 对象时采用了不同的方法。
我从 PHP 端创建该对象并将其存储在一个变量中,例如
$JSONvar
。现在我echo
该变量。然后在客户端,这就是我拦截对象的方式。
您的测试函数应该能够像这样访问该对象,
I had a different approach in parsing the JSON Object.
From the PHP side I create the object and store it in a variable say,
$JSONvar
. Now Iecho
the variable.Then on the client side this is how I intercept the object.
Your test function should be able to access the object like this,
从
xmlHttp.onreadystatechange
中的函数调用test()
,并将解码后的对象作为参数传递。Call
test()
from the function inxmlHttp.onreadystatechange
, passing the decoded object as a parameter.