如何将 Ajax.BeginForm MVC 助手与 JSON 结果一起使用? JSON 结果中的参考数据

发布于 2025-01-07 06:37:32 字数 705 浏览 0 评论 0原文

在帖子中 如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2025-01-14 06:37:32
function onTestSuccess(data, status, xhr) { 
    var uri = data.uri;
    // uri = '/Image/Confirm2?category=foo' at this stage 
    // so you could do something useful with it
} 

此外,要实现此功能,您需要在设置 Ajax.BeginForm 时使用 AjaxOptions 中的 OnSuccess="onTestSuccess" 设置,而不是 OnComplete="onTestSuccess"


事实证明,问题出在您的控制器操作中,您在其中指定了不正确的 text/html 内容类型。

因此,

String uri = Url.Action("Confirm2", "Image", new RouteValueDictionary(new { category = "foo" })); 
return Json(new { success = true, uri = uri }, "text/html");

您应该使用:

String uri = Url.Action("Confirm2", "Image", new RouteValueDictionary(new { category = "foo" })); 
return Json(new { success = true, uri = uri });
function onTestSuccess(data, status, xhr) { 
    var uri = data.uri;
    // uri = '/Image/Confirm2?category=foo' at this stage 
    // so you could do something useful with it
} 

Also for this to work you need to use the OnSuccess="onTestSuccess" setting in your AjaxOptions when setting up the Ajax.BeginForm instead of OnComplete="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:

String uri = Url.Action("Confirm2", "Image", new RouteValueDictionary(new { category = "foo" })); 
return Json(new { success = true, uri = uri }, "text/html");

you should use:

String uri = Url.Action("Confirm2", "Image", new RouteValueDictionary(new { category = "foo" })); 
return Json(new { success = true, uri = uri });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文