English 中文(简体)
Ajax.BeginForm,调用Action,返回JSON,如何在我的OnSuccessJS函数中访问JSON对象?
原标题:Ajax.BeginForm, Calls Action, Returns JSON, How do I access JSON object in my OnSuccess JS Function?

Ajax.BeginForm calls an action and then returns JSON. How do I access JSON object in my OnComplete js function?

所以我的<code>Ajax。BeginForm</code>看起来是这样的。。。

using (Ajax.BeginForm("Coupon", new AjaxOptions { OnSuccess = "CouponSubmitted" }))

我的OnSuccess函数如下所示。。。

function CouponSubmitted() {
    var data = response.get_response().get_object();
    alert(data.success);
}

我也试过。。。

function CouponSubmitted(data) {
    alert(data.success); 
}

我的控制器“优惠券”返回此。。。

return Json(new { success = false, nameError = nameError, emailError = emailError });

关于如何访问返回的Json,有什么想法吗

最佳回答
function OnSuccess(e) { //function CouponSubmitted(data) in the question
   var json = e.get_response().get_object();
   alert(json.success);
}

这就是AJAX.BeginForm OnSuccess回调所期望的恢复JSON的操作。

希望我能为其他人节省一些时间,让他们使用这个文档不足的“功能?”。

问题回答

我在ASP.NET MVC 4中遇到了这个问题,希望能找到答案来做同样的事情,但以上都不起作用,所以对于任何想找到答案的人来说,当你在js函数中收到数据时,数据已经从json编码了

 public ActionResult Something()
 {
    return Json(new { result = 0, message = "Testing" });
 } 

...

 new AjaxOptions { HttpMethod = "POST", OnSuccess= "something" }

...

function something(data) {
    switch(data.result)
    {
    case 1:
       alert(data.result)
    break;
    case 0:
        alert(data.result)
    break;
    case -1:
        alert(data.result)
    break;
    default:
        alert(data.message);
    }
}

这不适用于OnComplete我认为它没有接收数据的参数。

在asp.net中mvc 4

function CouponSubmitted(data) {
    alert(data.success); 
}

将返回解析后的json

这是一个自己做帖子的例子,但概念是一样的。请注意onsuccess函数的参数。该参数使您可以访问控制器返回的内容。如果是Json的数据,那么这就是你得到的。如果控制器返回了一个部分视图,那么您将获得该视图的html。您可以对返回的数据调用JQuery$.ParseJSON()函数。

$.post( /Assessment/GetAssessmentResults/  + SelectedId,   
function onsuccess(e) {  
   var json_object = $.parseJSON(e);  
}, "POST");  




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签