最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - How to use a JSON object received by one JavaScript function in another function? - Stack Overflow

programmeradmin2浏览0评论

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

 {
    "munication": [{
        "munication_name": "None",
        "munication_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(myJSONObjectmunication[0]munication_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?

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

 {
    "munication": [{
        "munication_name": "None",
        "munication_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.munication[0].munication_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?

Share Improve this question edited Nov 16, 2014 at 2:42 user3717023 asked Mar 25, 2011 at 4:23 Siva Siva 3,7488 gold badges29 silver badges28 bronze badges 1
  • 4 Longest stackoverflow sentence EVER – pc1oad1etter Commented Mar 25, 2011 at 14:45
Add a ment  | 

2 Answers 2

Reset to default 4

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 I echo the variable.

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);
}

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

发布评论

评论列表(0)

  1. 暂无评论