如何使用 Webmatrix / razor 获取单个 json 值?
我试图从一些 json 中获取单个值,并使用 Chrome 的 javascript 控制台来尝试和调试它。我尝试过 Request["id"] 和 Request.Params["id"] 但它没有抓取任何东西。我已经通过 chrome javascript 调试器验证了该值正在跨越,但我无法使用 webmatrix 提取该值。有人可以帮忙吗?
来自 Default.cshtml 的代码
$.ajax({
type: 'POST',
data: encodedItemId, //looks like { id : value }
dataType: 'json',
url: '/actions/act_markCompleted.cshtml',
success: function(data){
alert(data); //trying to view the json data
}
});
通过 ajax 发布到此页面,act_markCompleted.cshtml
@{
var itemId = Request["id"];
var data = "The id is: " + itemId;
var db = Database.Open("VacationBuddyDB");
var sql = "UPDATE Items SET completed = @0 WHERE Id = @1";
db.Execute(sql, true, itemId.AsInt());
Json.Write(data, Response.Output);
}
I am trying to grab just a single value from some json and am using Chrome's javascript console to try and debug it. I have tried Request["id"], and Request.Params["id"] and it doesn't grab anything. I have verified via the chrome javascript debugger that the value is going across but I can't pull the value out using webmatrix. Can anybody please help?
code from Default.cshtml
$.ajax({
type: 'POST',
data: encodedItemId, //looks like { id : value }
dataType: 'json',
url: '/actions/act_markCompleted.cshtml',
success: function(data){
alert(data); //trying to view the json data
}
});
Posts via ajax to this page, act_markCompleted.cshtml
@{
var itemId = Request["id"];
var data = "The id is: " + itemId;
var db = Database.Open("VacationBuddyDB");
var sql = "UPDATE Items SET completed = @0 WHERE Id = @1";
db.Execute(sql, true, itemId.AsInt());
Json.Write(data, Response.Output);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下内容应该有效:
确保您没有将
{ id : 'value' }
作为字符串发送,而是作为 javascript 对象发送,如图所示。另外,如果您在某些链接或表单提交的单击操作的回调中运行此 ajax 请求,请确保通过在最后返回 false 来取消默认操作,否则 ajax 请求可能没有时间执行。The following should work:
Make sure that you are not sending
{ id : 'value' }
as a string but rather as a javascript object as shown. Also if you are running this ajax request inside the callback of the click action of some link or form submit make sure that you cancel the default action by returning false at the end or the ajax request might not have time to execute.