jQuery Ajax.BeginForm,调用操作,返回 JSON,如何在我的 OnSuccess JS 函数中访问 JSON 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4020705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:31:04  来源:igfitidea点击:

Ajax.BeginForm, Calls Action, Returns JSON, How do I access JSON object in my OnSuccess JS Function?

asp.net-mvcjsonjquery

提问by Jason

Ajax.BeginFormcalls an action and then returns JSON. How do I access JSON object in my OnCompletejs function?

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

so my Ajax.BeginFormlooks like this...

所以我的Ajax.BeginForm样子...

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

and my OnSuccessfunction 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 的任何想法?

回答by Jason

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?".

希望我在这个可笑的记录下的“功能?”上为其他人节省了一些时间。

回答by formatc

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 OnCompleteI assuame it doesn't have paramtars to recive data.

这不适用于 OnComplete我假设它没有参数来接收数据。

回答by Mudasar Rauf

in asp.net mvc 4

在 asp.net mvc 4 中

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

will return parsed 'json'

将返回解析的“json”

回答by Kevin Koenig

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");