如何将 Ajax.BeginForm MVC 助手与 JSON 结果一起使用? JSON 结果中的参考数据
在帖子中 如何使用 Ajax.BeginForm MVC 助手JSON 结果? Joel 引用了如下解决方案:
function onTestSuccess(data, status, xhr) {
console.log("data", data);
console.log("xhr", xhr);
console.log("status", status);
// Here's where you use the JSON object
//doSomethingUseful(data);
}
How do I reference elements in the JSON object "data" in my code? 我的控制台日志显示以下内容: LOG: data{"success":true,"uri":"/Image/Confirm2?category=foo"}
我试图在我的 jquery 代码中使用“uri”的值。我尝试过:
console.log("uri", data.uri);
但得到以下结果:
LOG: datauriundefined
In the post How to use Ajax.BeginForm MVC helper with JSON result? Joel references a solution as follows:
function onTestSuccess(data, status, xhr) {
console.log("data", data);
console.log("xhr", xhr);
console.log("status", status);
// Here's where you use the JSON object
//doSomethingUseful(data);
}
How do I reference elements in the JSON object "data" in my code?
My console log shows the following:
LOG: data{"success":true,"uri":"/Image/Confirm2?category=foo"}
I am trying to use the value of "uri" in my jquery code. I've tried:
console.log("uri", data.uri);
but get the folowing as a result:
LOG: datauriundefined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此外,要实现此功能,您需要在设置
Ajax.BeginForm
时使用 AjaxOptions 中的OnSuccess="onTestSuccess"
设置,而不是OnComplete="onTestSuccess"
。事实证明,问题出在您的控制器操作中,您在其中指定了不正确的
text/html
内容类型。因此,
您应该使用:
Also for this to work you need to use the
OnSuccess="onTestSuccess"
setting in your AjaxOptions when setting up theAjax.BeginForm
instead ofOnComplete="onTestSuccess"
.It turns out that the problem is with your controller action in which you specified incorrect Content-Type of
text/html
.So instead of:
you should use: