【发布时间】:2013-04-11 08:12:32
【问题描述】:
我正在调试我的 javascript 代码(如下)。
在用户单击搜索按钮后填充网络网格。我在 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 方法的值。
【问题讨论】:
-
听起来响应头 content-type 没有设置为 application/json
-
感谢您的回复。我已经设置了我的 $.ajax({ type : "POST", contentType : "application/json" 等;但仍然没有将数据绑定到输入文本,也没有 JSON 选项卡。
-
这将设置请求标头,而不是响应标头。必须在服务器端设置响应标头。你能展示一些服务器端代码吗?
-
向我们展示您的 GetRecipients 方法
-
@CristiPufu 我已经编辑了问题并包含了 GetRecipients 方法。
标签: javascript razor getjson webmatrix