2

我正在调试我的 javascript 代码(如下)。

用户单击搜索按钮后,将填充 webgrid。我在 webgrid 中添加了一个按钮,该按钮会打开一个对话框,该对话框必须使用来自 JSON 对象的值进行填充。

这就是问题所在 - 当我使用 firebug 进行调试时,控制台中的 JSON 选项卡未显示。

以下是我的代码的一部分:

 $('.edit-recipients').live('click', function ()
           {
               $.getJSON('/Methods/GetRecipients/' + $(this).attr('id'), function (data)
               {
                   var recipient = data;
                   console.log(recipient);
               $('#edit-opno').val(recipient.OpNo);

Console.log(recipient) 显示我的GetRecipients方法中的值。
此代码 $('#edit-opno').val(recipient.OpNo);旨在显示我在下面有此代码的输入文本中的值。

<input type="text" name="opno" id="edit-opno" size="15" />

但是起初我认为 GetRecipients 没有执行,但从萤火虫意识到它是使用 console.log(recipients) 执行的,显示值但没有 JSON 选项卡,因此无法填充我的对话框输入框。

以下是我的服务器端代码:

@{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    if(UrlData[0].IsInt()){
        var db = Database.Open("sb_cpd");
        var sql = "SELECT * FROM cpd_recipients WHERE ID = @0";
        var recipients = db.QuerySingle(sql,UrlData[0]);
        Json.Write(recipients, Response.Output);
    }
}

我已经插入了正在发生的事情的图像。请注意,我的对话框没有填充来自 GetRecipients 方法的值。 在此处输入图像描述

4

1 回答 1

2

您应该将标头内容类型设置为 application/json。然后 Firebug 会将响应识别为 JSON:

@{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    if(UrlData[0].IsInt()){
        var db = Database.Open("sb_cpd");
        var sql = "SELECT * FROM cpd_recipients WHERE ID = @0";
        var recipients = db.QuerySingle(sql,UrlData[0]);
        Response.Headers.Add("Content-type", "application/json");
        Json.Write(recipients, Response.Output);
    }
}
于 2013-04-11T08:47:28.460 回答