GET返回未定义的值输出函数 [英] GET return undefined value out function

查看:98
本文介绍了GET返回未定义的值输出函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有全局变量fan_coil_ai_bi,我使用该变量来存储我从请求获得的一些数据,但问题是len_1是1(在这种情况下是什么值)和len_2未定义(什么是错误的)。如何在我的功能中发生这种情况?如何实现len_2具有与len_1相同的值?这段代码有什么问题?

I have global variable fan_coil_ai_bi, and I use that variable to store some data which I get from request, but problem is that len_1 is 1 ( what is ok value in this case ) and len_2 is undefined (what is wrong). How that happen in my function bellow ? How to achieve that len_2 have same value like len_1 ? What is wrong with this code ?

function read_required_fields(fan_coil_id) {
    var parameters = {};

    parameters['command'] = 'read_required_fields';
    parameters['fan_coil_id'] = fan_coil_id;

    $.get("php_scripts/network_script.php", parameters, function(data) {
        fan_coil_ai_bi=data;
        alert('len_1='+fan_coil_ai_bi.length);
    }, "json");
    alert('len_2='+fan_coil_ai_bi.length);
}


推荐答案

异步JavaScript和XML是异步

Asynchronous JavaScript and XML is asynchronous.

get 方法的意思是发出HTTP请求以及何时获取响应,执行此操作。

The get method means "Make an HTTP get request and when it gets a response, do this".

在HTTP响应到达之前,它不会暂停执行该功能。

It does not pause execution of the function until the HTTP response arrives.

如果您想对数据执行任何操作,请在回调函数中 (或您从中调用的函数)。

If you want to do anything with the data, do it in the callback function (or a function you call from it).

$.get("php_scripts/network_script.php", parameters, function(data) {
    fan_coil_ai_bi=data;
    alert('len_1='+fan_coil_ai_bi.length);
    alert('len_2='+fan_coil_ai_bi.length);
}, "json");

这篇关于GET返回未定义的值输出函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆