【问题标题】:Passing an list to Ajax将列表传递给 Ajax
【发布时间】:2014-07-23 07:21:59
【问题描述】:
public List<double> GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";

            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

            var cordinates = new List<double>();

            foreach (var result in googleResults.results)
            {

                cordinates.Add(result.geometry.location.lng);

                cordinates.Add(result.geometry.location.lat);

            }

            return cordinates;

        }

现在,我想要两个在我的 Ajax 中使用列表中的值:

 $(function() {
                    $('#ajax').click(function() {
                        $.ajax({

                                url: '@Url.Action("GoogleGeoCode", "Home")',
                                context: document.body
                            })
                            .done(function(serverdata) {
                                data = serverdata;
                                $.each(data, function(i, item) {
                                    var marker = new google.maps.Marker({
                                        'position': new google.maps.LatLng(item[0], item.GeoLat[1]), <-- I thought Item would contain my list and I could use its index
                                        'map': map,
                                        'title': item.PlaceName,
                                        'Catch': item.Catch

                                    });
                                });
                            });
                    });

我是 Ajax 的初学者,如果有人有时间,我将非常感谢逐步解释它是如何工作的。谢谢!

更新: 新课程:

public class LongLatModel
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

控制器:

public LongLatModel GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";

            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

            var cordinates = new List<double>();

            var longlat = new LongLatModel();


            foreach (var result in googleResults.results)
            {

                longlat.Latitude = result.geometry.location.lng;

                longlat.Longitude = result.geometry.location.lat;

            }

            return longlat;

        }

阿贾克斯:

$(function() {
                    $('#ajax').click(function() {
                        $.ajax({
                                // in a real scenario, use data-attrs on the html
                                // rather than linking directly to the url
                                url: '@Url.Action("GoogleGeoCode", "Home")',
                                context: document.body
                            })
                            .done(function(serverdata) {
                                data = serverdata;
                                $.each(data, function(i, item) {
                                    var marker = new google.maps.Marker({
                                        'position': new google.maps.LatLng(data.longitude, data.latitude),
                                        'map': map,
                                        'title': item.PlaceName,
                                        'Catch': item.Catch

                                    });
                                });
                            });
                    });

编辑:

好的,所以我设法将正确的信息输入 AJAX(我认为..) 但是,我很难确定如何将这两个值放在它们应该在的位置。

我怎样才能循环并将它们放置在正确的位置?

$.each(数据, 函数 (i, item) {

var 标记 = 新的 google.maps.Marker({ '位置': 新的 google.maps.LatLng(LONG, LAT),

});

【问题讨论】:

    标签: ajax asp.net-mvc


    【解决方案1】:
    1. 在 C# 方法中,您应该为 LatLog 创建数据结构,而不是在方法中返回 List

      class LatLongModel
      {
          public double Latitude {get;set;}
          public double Longitude{get;set;}
      }
      
    2. 在 JavaScript 中,在 done 方法中,您可以检查:

      data.Longitude; data.Latitude;
      
    3. 您还应该在 Ajax 请求中添加失败,以防请求失败。看看这个sn-p:http://jsfiddle.net/JhWt4/

    更新

    在 C# 代码中你要创建并返回一个 List

        public class GeoCodeResultModel
        {
            public double Latitude { get; set; }
            public double Longitude { get; set; }
            public string PlaceName { get; set; }
            public string Catch { get; set; }
        }
    
        public List<GeoCodeResultModel> GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
    
            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();
    
            var locations = new List<GeoCodeResultModel>();
    
            foreach (var result in googleResults.results)
            {
                locations.Add(new GeoCodeResultModel
                {
                     Longitude = result.geometry.location.lng,
                     Latitude = result.geometry.location.lat,
                     PlaceName = 'TODO',
                     Catch = 'TODO'
                }
            }
    
            return locations;
    
        }
    

    对于 JavaScript 代码:

    $.each(data, function(i, item) {
        var marker = new google.maps.Marker({
                     'position': new google.maps.LatLng(item.Latitude, item.Longitude), 
                     'map': map,
                     'title': item.PlaceName,
                     'Catch': item.Catch
        });
    });
    

    免责声明:这不是经过测试的代码,它旨在为您指明正确的方向。

    【讨论】:

    • 所以我应该创建类 LatLongModel 然后在方法中创建它的实例并将模型传递给 Js?顺便说一句,感谢您的宝贵时间!
    • 是的。创建那个类,用正确的值实例化它,然后在方法中返回实例。
    • @user2915962 您是否期望 googleResults.results 有多个结果?
    • 此时不,我现在已经创建了将两个值(long/lat)传递给 Ajax 的模型。请看我的更新。不知道 ajax 是否正确。
    • @user2915962 你可以做得更好,看看 Google Chrome 中的 JavaScript 调试。 developer.chrome.com/devtools/docs/javascript-debugging。按 F12 打开开发工具。在源选项卡中,您可以在代码上设置断点。一旦断点命中,您可以在右侧面板或控制台中观察值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多