简体   繁体   English

尝试将Ajax JSON响应存储到变量中时出现未定义错误

[英]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. 我正在尝试进行ajax调用,我想将JSON响应存储到变量中。
Because the JSON response, i want to pass it to two Jqgrids . 因为是JSON响应,所以我想将其传递给两个Jqgrids One will display half of the response and another will display remaining response. 一个将显示一半的响应,另一个将显示剩余的响应。 Please help me with some idea to display on Jqgrids . 请帮我一些想法在Jqgrids上显示。

This is how i am trying to store JSON response, but i am getting undefined error while passing variable to alert() function. 这就是我试图存储JSON响应的方式,但是在将变量传递给alert()函数时遇到了undefined error

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从文件中读取整数并将其存储在变量中时出现输入不匹配错误 - Getting an input mismatch error when trying to read an integer from a file and store it in a variable 尝试从Json Web响应获取Json节点时出错 - Error when trying to get Json node from a Json Web Response Javascript,Ajax和JSON:解析响应时出现奇怪的“ xml处理”错误 - Javascript, Ajax & JSON: weird “xml processing” error when parsing response 尝试从 json 响应中获取节点时出现 java 错误 - java error when trying to fetch node from json response 尝试发送 post rquest 时出现无效的 json 错误 - Getting invalid json error when trying to send post rquest 尝试使用GSON反序列化JSON字符串时出现错误 - Getting an error when trying to deserialize a JSON string with GSON 尝试访问swagger.json时出现404错误 - Getting 404 error when trying to access swagger.json 尝试使用 Webhook On Dialogflow 动态提供响应时出现 Webhook 响应错误 (206) - getting Webhook response error (206) when trying to give response Dynamically using webhook On Dialogflow 尝试解析 JSON 字符串时出错。 org.json.JSONException:JSONArray 文本必须以 '[' 开头 - Getting error when trying parse JSON string. org.json.JSONException: A JSONArray text must start with '[' 尝试读取响应正文时获取服务器响应 405 - Getting server response 405 when trying to read response body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM