20

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

so my Ajax.BeginForm looks like this...

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

and my OnSuccess function looks like this...

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?

Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
Jason
  • 11,435
  • 24
  • 77
  • 131

4 Answers4

34
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.

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

Jason
  • 11,435
  • 24
  • 77
  • 131
  • It seems MVC3 with unobtrusive ajax uses a different approach. The first parameter for OnSuccess is the data object. – pauloya Oct 21 '11 at 11:05
  • 8
    [`CouponSubmitted(data, status, xhr)`](http://stackoverflow.com/a/7467032/907779) in [tag:asp.net-mvc3]. – Joel Purra Feb 19 '12 at 11:07
  • @Joel - Thanks. Is this documented anywhere? ASP.NET MVC seems horribly undocumented. – Justin Helgerson Mar 27 '12 at 15:13
  • @Ek0nomik: Couldn't find any documentationon on javascript callbacks with a quick google search (2011-09), but see my linked answer for [two out of three callback signatures](http://stackoverflow.com/a/7467032/907779). Found them reading `~/Scripts/jquery.unobtrusive-ajax.js`, which gives you the real scoop :) – Joel Purra Mar 27 '12 at 15:49
  • using MVC v4.0.30319, you can get it using get_objet() without get_response() – Samih A Dec 11 '14 at 08:28
14

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

 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.

formatc
  • 4,261
  • 7
  • 43
  • 81
  • Thank you thank you thank you thank you. Took me so long to find out how to do this. I just needed that OnSuccess – Keerigan Oct 20 '12 at 15:41
  • 1
    That last note about OnComplete needs to be bigger. This was my problem as well. – Joe Feb 01 '13 at 20:29
2

in asp.net mvc 4

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

will return parsed 'json'

Mudasar Rauf
  • 544
  • 7
  • 18
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.

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