简体   繁体   中英

Getting Undefined Error when trying to store ajax JSON response into variable

I am trying to make ajax call and i want to store the JSON response into variable.
Because the JSON response, i want to pass it to two Jqgrids . One will display half of the response and another will display remaining response. Please help me with some idea to display on Jqgrids .

This is how i am trying to store JSON response, but i am getting undefined error while passing variable to alert() function.

var form_data;
$(searchBTN).click(function(event))
{
    $.getJSON("search?dealId=" + orderId,
        function(json) {
            form_data = json.CompanyName;
            checkdata();                                        
        });

    function checkData() {
        console.log(form_data);
        alert(form_data);
    }
}

请检查您的json数据格式,意味着json数组变量键。我认为您访问了错误的变量。未定义错误表明您当前的键在json对象中未定义。要进行测试,请使用此alert(JSON.parse(json))

A better way to do what I think you intend is as follows:

var form_data;
$(searchBTN).click(function(event)) {
    $.getJSON("search?dealId=" + orderId, function(json){
        form_data = json.CompanyName;
        console.log(form_data);
        alert(form_data);               
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM