【问题标题】:jQuery.ajax, json, and vb.net javascriptserializejQuery.ajax、json 和 vb.net
【发布时间】:2011-11-04 14:33:26
【问题描述】:

我在一些非常简单的 json 上玩得很开心,希望有人能提供帮助。我有一个非常基本的 VB.NET 网络表单,它写出用 JavaScriptSerializer 序列化的数据表的内容。

        Using conn As New SqlConnection(connStr)
        Using command As New SqlCommand("select * from stategeopositions", conn)
            conn.Open()
            Using reader As SqlDataReader = command.ExecuteReader()
                dt.TableName = "states"
                dt.Load(reader)
                conn.Close()
            End Using
        End Using
    End Using

    Dim objs As New List(Of Dictionary(Of String, Object))
    For Each row As DataRow In dt.Rows
        Dim drow As New Dictionary(Of String, Object)
        For i As Integer = 0 To dt.Columns.Count - 1
            drow.Add(dt.Columns(i).ColumnName, row(i))
        Next
        objs.Add(drow)
    Next

    Dim d As New Dictionary(Of String, Object)
    d.Add(dt.TableName, objs)

    Dim serialier As New JavaScriptSerializer

    Response.Write(serialier.Serialize(d))

在 jquery 方面我正在这样做。

            $.ajax({
            type: "GET",
            url: 'StateGeoPositions.aspx',
            async: false,
            beforeSend: function (x) {
                if (x && x.overrideMimeType) {
                    x.overrideMimeType("application/j-son;charset=UTF-8");
                }
            },
            dataType: "json",
            success: function (data) {
                alert(data);
                //do your stuff with the JSON data
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });

这是返回“无效的 json”

这是输出

{"states":[{"state":"Alaska","abbreviation":"AK","latitude":61.370717,"longitude":-152.404420,"small":null},{"state" :"Alabama","abbreviation":"AL","latitude":32.806673,"longitude":-86.791133,"small":null},{"state":"Arkansas","abbreviation":"AR", "纬度":34.969705,"经度":-92.373124,"小":null},{"州":"亚利桑那州","缩写":"AZ","纬度":33.729761,"经度":-111.431224, "small":null},{"state":"California","abbreviation":"CA","latitude":36.116203,"longitude":-119.681563,"small":null},{"state":" Colorado","abbreviation":"CO","latitude":39.059810,"longitude":-105.311105,"small":null},{"state":"Connecticut","abbreviation":"CT","latitude ":38.272689,"经度":-71.367188,"small":true},....

提前感谢您的帮助...

【问题讨论】:

  • 请提供您所有的 JSON 输出,因为您当前包含的内容是有效的语法。
  • 对不起,我不确定你的意思?这只是它的结束。这里有 1 条记录。我在页面加载中执行此操作,并且仅使用 Reponse.Write(..) {"":[{"state":"Alaska","abbreviation":"AK","latitude":61.370717,"longitude": -152.404420,"small":false}]}

标签: jquery vb.net json


【解决方案1】:

在这种情况下,您应该可以使用速记函数$.getJSON()

var url = 'StateGeoPositions.aspx';

$.getJSON(url, function(data){
    alert(data);
});

【讨论】:

  • 仍然没有返回任何内容...... json 输出看起来正确吗?什么是领先的“状态”标签?
  • 我发现了问题...我将 HTML 正文标记留在了 aspx 文件中。我把这个文件剥离干净并且能够让它工作。感谢大家的帮助
猜你喜欢
  • 2015-11-07
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
相关资源
最近更新 更多