【问题标题】:$.getJSON doesn't read my .aspx return$.getJSON 没有读取我的 .aspx 返回
【发布时间】:2012-05-12 02:48:25
【问题描述】:

所以我搜索了大多数 getJSON 问题,但仍然无法找到解决问题的方法。我的主要问题是:我有一个对 .aspx 页面进行 $.getJSON 调用的 .js 文件。 .aspx 页面返回一个 json 对象。我已经用 demo.js 测试了 $.getJSON,它工作得很好;我能够引用 json 字段。 .aspx 页面不是这种情况。这是我的代码:

.js 调用 $getJSON

$.getJSON('updateSlides.aspx', function (json) {
    alert("JSON Data: " + json.url);
});

.aspx 返回 json obj

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim jsonString As String
    If Not Page.IsPostBack Then
        Dim ws As New wallboardSettings
        ' pull data values from db here, replace hardcoded values below
        ws.duration = 5
        ws.imagePath = "Images\slide1.jpg"
        ws.url = "slide1.html"
        Dim js As JavaScriptSerializer = New JavaScriptSerializer()
        jsonString = js.Serialize(ws)
        Response.Write(jsonString)
    End If
End Sub

我在 VB 中放置了一个 msgbox,并从我的本地机器上运行它来查看我的值。它以 json 形式返回 ws 属性设置,该设置在 jsonlint.com 上正确验证。

我也尝试过使用带有 async: false 的 $.ajax 调用,但它不起作用。当我使用有效的 demo.js 时,我在 firebug 中看到了 json 字段; updateSlides.aspx 不是这种情况。

提前致谢, 布赖恩

【问题讨论】:

  • 你试过直接调用aspx吗?

标签: json javascriptserializer getjson


【解决方案1】:

从您的响应中将内容类型标头设置为 application/json。

【讨论】:

    【解决方案2】:

    所以事实证明 json 不是问题所在。 aspx 页面返回 json 就好了。根据萤火虫相同的aspx页面也返回自动生成的附加html。我删除了这个 html,firebug 显示了 json 数据。感谢您的回复!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多