JQUERY Ajax Get Method获取数据属性

时间:2015-11-18 11:29:21

标签: javascript jquery ajax

我在Javascript中使用JQUERY Ajax方法来获取网关URL的get请求的数据,并将其中的一些属性值发送到现有对象的属性。我相信有一个简单的方法可以解决它。下面是代码,我希望那里的人可以提供帮助。

<!DOCTYPE html>
<html>
<head>
  <title>Gateway Object</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-
              scale=1.0">
  <script src="jquery-2.1.4.js"></script>
  <script>
    gateway = {
        ip: "",
        hwv: "",

        setIpAddress: function (ip) {
            var self = this;
            self.ip = ip;
        },

        getHardwareVersion: function () {
            var self = this;
            $.ajax({
                type: "GET",
                url: "http://" + self.ip + "/command?XC_FNC=GetSI",
                timeout: 2000,
                error: function (err) {
                    console.log("gateway error: check ip address and
                                try again");
                },
                success: function (data) {
                    if (data) {
                        if (data.substr(0, 8) === "{XC_SUC}") {
                            var jString = (data.slice(8));
                            var obj;
                            try {
                                obj = JSON.parse(jString);
                            } catch (e) {
                            }
                            self.hwv = obj.HWV;
                            //console.log(self.hwv);
                        }
                        else {
                            console.log("Error:" + "" + data);
                        }
                    }
                    else {
                        console.log("error with the gateway");
                    }
                }
            });
        }
      };
       gateway.setIpAddress("200.201.51.126");
       gateway.getHardwareVersion();
       console.log(gateway);
   </script>
</head>
<body></body>
</html>

这似乎工作正常但是“gateway.hwv”在ajax请求之后无法接收数据对象的属性。是否可以通过Ajax-Asynchronous方法执行此操作?

2 个答案:

答案 0 :(得分:1)

使用console.log(网关);直接在gateway.getHardwareVersion()之后;不会像你期望的那样工作,因为日志将在你的ajax查询处理完毕之前运行。

您需要通过同步更改ajax函数或运行从其成功函数调用ajax后需要运行的任何逻辑。

答案 1 :(得分:0)

您可以将complete函数添加到Ajax并调用其中的console.log。因此,只有在异步Ajax请求完成时,它才会向您显示gateway对象。

一个示例(我将self.hwv设置为1只是为了填充它):

<script>
    gateway = {
            ip: "",
            hwv: "",

            setIpAddress: function (ip) {
                    var self = this;
                    self.ip = ip;
            },

            getHardwareVersion: function () {
                    var self = this;
                    $.ajax({
                            type: "GET",
                            url: "http://" + self.ip + "/command?XC_FNC=GetSI",
                            timeout: 2000,
                            error: function (err) {
                                    console.log("gateway error: check ip address and try again");
                            },
                            success: function (data) {
                                    if (data) {
                                            if (data.substr(0, 8) === "{XC_SUC}") {
                                                    var jString = (data.slice(8));
                                                    var obj;
                                                    try {
                                                            obj = JSON.parse(jString);
                                                    } catch (e) {
                                                    }
                                                    self.hwv = obj.HWV;
                                                    //console.log(self.hwv);
                                            }
                                            else {
                                                    console.log("Error:" + "" + data);
                                            }
                                    }
                                    else {
                                            console.log("error with the gateway");
                                    }
                            },
                            complete: function() {
                                self.hwv = "1";
                                console.log(gateway);
                            }
                    });
            }
        };
        gateway.setIpAddress("localhost");
        gateway.getHardwareVersion();
 </script>