Ajax.BeginForm,调用Action,返回JSON,如何访问OnSuccess JS函数中的JSON对象?

时间:2022-08-02 14:28:56

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

Ajax.BeginForm调用一个动作,然后返回JSON。如何在OnComplete js函数中访问JSON对象?

so my Ajax.BeginForm looks like this...

所以我的Ajax.BeginForm看起来像这样......

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

and my OnSuccess function looks like this...

我的OnSuccess功能看起来像这样......

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

I also tried...

我也试过......

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

My controller "Coupon" returns this...

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

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

Any ideas on how to access the Json that gets returned?

关于如何访问返回的Json的任何想法?

4 个解决方案

#1


33  

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

This is what the AJAX.BeginForm OnSuccess callback expects you to do to get your JSON back.

这就是AJAX.BeginForm OnSuccess回调期望你做的事情,以恢复你的JSON。

Hope I saved someone else some time on this ridiculously under documented "feature?".

希望我在一段时间内将这个可笑的“特征”下载给别人一些时间。

#2


13  

I bumped into this question looking for the answer to do the same thing in ASP.NET MVC 4, and none of the above worked, so for anyone looking for the answer, the data is already encoded from json when you recive it in your js function

我遇到了这个问题,寻找在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);
    }
}

This doesn't work with OnComplete I assuame it doesn't have paramtars to recive data.

这不适用于OnComplete我认为它没有paramtars来重现数据。

#3


2  

in asp.net mvc 4

在asp.net mvc 4中

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

will return parsed 'json'

将返回解析'json'

#4


0  

this is an example of doing the post yourself but the concept is the same. Notice the parameter to the onsuccess function. the parameter gives you access to whaever the controller returned. If it is Json data then that is what you get. If the controler returned a partial view then you get the html for the view. You can call the JQuery $.ParseJSON() function on the returned data.

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

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

#1


33  

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

This is what the AJAX.BeginForm OnSuccess callback expects you to do to get your JSON back.

这就是AJAX.BeginForm OnSuccess回调期望你做的事情,以恢复你的JSON。

Hope I saved someone else some time on this ridiculously under documented "feature?".

希望我在一段时间内将这个可笑的“特征”下载给别人一些时间。

#2


13  

I bumped into this question looking for the answer to do the same thing in ASP.NET MVC 4, and none of the above worked, so for anyone looking for the answer, the data is already encoded from json when you recive it in your js function

我遇到了这个问题,寻找在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);
    }
}

This doesn't work with OnComplete I assuame it doesn't have paramtars to recive data.

这不适用于OnComplete我认为它没有paramtars来重现数据。

#3


2  

in asp.net mvc 4

在asp.net mvc 4中

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

will return parsed 'json'

将返回解析'json'

#4


0  

this is an example of doing the post yourself but the concept is the same. Notice the parameter to the onsuccess function. the parameter gives you access to whaever the controller returned. If it is Json data then that is what you get. If the controler returned a partial view then you get the html for the view. You can call the JQuery $.ParseJSON() function on the returned data.

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

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