1

我在 document.ready 中有下面的代码,当我使用 Chrome 的调试器查看为什么没有发生任何事情时,我发现由于某种原因,ajax 调用被跳过了?

var latitude = $('#LatitudeHidden').val();
                var longitude = $('#LongitudeHidden').val();
                var from = $('#<%:FromTextBox.ClientID %>').val();
                var to = $('#<%:ToTextBox.ClientID %>').val();
                var type = $('#<%:TypeEnhancedDropDownList.ClientID %>').val();
                var specialLocation = $('#<%:SpecialLocationsEnhancedDropDownList.ClientID %>').val();

                var json = {
                    'latitude': latitude,
                    'longitude': longitude,
                    'from': from,
                    'to': to,
                    'type': type, 
                    'specialLocation': specialLocation
                };

                $.ajax({
                    type: "POST",
                    url: "List.aspx/GetFilteredLocations",
                    data: json,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    error: function(msg)
                    {
                        console.log(msg);
                    },
                    success: function(msg)
                    {
                        console.log(msg.d);
                    }
                });
4

1 回答 1

2

It seems like it is having trouble deserializing the json data... You may need to stringify your data, even though you do have the datatype set to json. There is a js library to accomplish this. Here is JSON js library to help

Try this (after including the JSON library into your page)

 $.ajax({
     type: "POST",
     url: "List.aspx/GetFilteredLocations",
     data: JSON.stringify(json),
     error: function(msg)
     {
        console.log(msg);
     },
     success: function(msg)
     {
        console.log(msg.d);
     }
 });
于 2012-07-19T20:15:39.107 回答