【问题标题】:How to store json data in varibe using ajax calls如何使用ajax调用将json数据存储在变量中
【发布时间】:2016-08-30 06:59:15
【问题描述】:

我希望使用 ajax 调用将 JSON 数据存储在变量中。如何编写 ajax 调用并将 JSON 数据值保存在变量中以在 Google 地图中显示数据。需要来自 JSON 的DustValue,并且应该在 Google Maps js 的变量中分配。

<head>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCLi6lY_WbRa9mkWKc" type="text/javascript"></script>
    <script>
        var myCenter = new google.maps.LatLng(22.8046, 86.2029);
        function initialize()
        {
            var mapProp = {
                center:myCenter,
                zoom:17,
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };

            var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
            var DustValue=23;//  ajax call.. $.ajax({url: 'http://xyz.in/dust/get'......
            var dustbinstatus='';

            if(DustValue<50){
                dustbinstatus = 'img/dustbinempty.png';
            } else if(DustValue>50 && DustValue<90){
                dustbinstatus = 'img/dustbinfull.png';
            } else if(DustValue>90){
                dustbinstatus = '3.jpg';
            }

            var marker=new google.maps.Marker({
                position:myCenter,
                icon: v_icon
            });

            marker.setMap(map);
        }

        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>

<body>
    <div id="googleMap" style="width:1580px;height:780px;"></div>
</body>

样本数据:

{"success":true,"sensorsdata":{"WaterTemperature":null,"DustValue":23.00,"DOValue":null,"CurrentTime":"29-August-2016 02:59:AM"}}

【问题讨论】:

    标签: javascript json ajax google-maps asp.net-ajax


    【解决方案1】:

    您需要使用success 回调处理程序将响应存储在变量中,这是一个示例,

        var dustValue;    
        $.ajax({
              type: "GET",
              url: "your_url_goes_here",
              dataType: "json",
              success : function(data) {
                  dustValue = data;
               }
            });
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多