1

I have having such a hard time getting my json variables out of my json that is retuned from my ajax call to my asp.net mvc controller class. Here is my jquery ajax call:

$.ajax({
 url: '@Url.Action("ReturnTotals", "Cart")',
 type: 'POST',
 datatype: 'json',
 success: function (jsonData) {
 var totals = $.parseJSON(jsonData);
 var cost = totals.cost;
 var qty = totals.qty;
 $("#totalCost").html(cost);
 $("#totalQty").html(qty);
 console.log(cost);
 console.log(qty);
 console.log($.parseJSON(jsonData));
 }
 });

My first 2 console.logs come back undefined. And the third spits out

[Object { Key="cost", Value=109}, Object { Key="qty", Value=12}]

I have checked that line with jsonlint and it is well formed json. For the line "var cost = totals.cost;" I have tried changing that to "var cost = totals["cost"];" and that didn't do anything.

Here is my server side function:

public string ReturnTotals()
 {
 CartSummaryModel cart = null;
 CartModel m = null;
 cart = (CartSummaryModel)cXML.cXML.DeserializeObject(Session["CARTSUMMARY"].ToString(), typeof(CartSummaryModel));
 Dictionary<string, double> totals = new Dictionary<string, double>();
 totals.Add("cost", cart.TotalCost);
 totals.Add("qty", cart.TotalQty);
 string totalString = cXML.cXML.SerializeJson( totals );
 return totalString;
 }
Bellash
8,2576 gold badges57 silver badges94 bronze badges
asked Apr 8, 2014 at 16:01
2
  • You are returning an array so to access it write total[0].cost Commented Apr 8, 2014 at 16:05
  • still returns 'undefined' Commented Apr 8, 2014 at 16:45

1 Answer 1

1

There is a default Json serialiser within assp.net mvc that you can use! In fact, I am not trying to discourage you from using your serializer but here is what works for me

 public JsonResult ReturnTotals()
 {
 CartSummaryModel cart = null;
 CartModel m = null;
 // cart = (CartSummaryModel) Session["CARTSUMMARY"] ;
 Dictionary<string, double> totals = new Dictionary<string, double>();
 totals.Add("cost", cart.TotalCost);
 totals.Add("qty", cart.TotalQty);
 return Json(totals,"text/json",JsonBehavour.AllowGet);
 }

And jQuery

 $(document).ready(function(){
 var url='@Url.Action("ReturnTotals", "Cart")';
 $.getJSON(url).done(function (jsonData) {
 var totals =[]||jsonData;
 var cost = totals.cost;
 var qty = totals.qty;
 $("#totalCost").html(cost);
 $("#totalQty").html(qty);
 console.log(cost);
 console.log(qty);
 }); 
 });
answered Apr 8, 2014 at 18:12
Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Something must have been wrong with the json I was passing using my old method. Thank you much!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.