Red Swan
Red Swan

Reputation: 15545

how to get json data in jquery var?

I am trying to get grid on view, for this I am using the jqgrid. and my action controller methods is returning me json data. I want to pick this data up in the 'var. in jquery. I am using asp.net mvc. how can i get this I tried :

$.getJSON(url:gridDataUrl,{}, function(jsonData) {
  alert(jsonData);
});
); 

Where gridDataUrl having my action url. how to do this?

Upvotes: 0

Views: 913

Answers (2)

Alexander Taran
Alexander Taran

Reputation: 6725

$.getJSON("@Url.Action("Search")", $("#jsonform").serialize(), function (data) {
   $("#results").html("");
   $("#phoneTemplate").tmpl(data).appendTo("#results");
   });
return false;
});

I use this code along with template plugin for jquery to get json data from an action and render it on the client.

are you hiting your action with ajax call? do you construct your json with JsonRequestBehavior.AllowGet set?

return Json(yourdata ,JsonRequestBehavior.AllowGet);

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630359

The $.getJSON() method signature is

jQuery.getJSON(url, [data], [callback(data, textStatus, xhr)])

...so it should look like this:

$.getJSON(gridDataUrl, function(jsonData) {
  alert(jsonData);
});

Note that the first param is the URL as just a string (not a label) and that both data and the callback are optional (denoted by [] in the signature). The above should only alert [object Object] though, since it's the overall object...you'll need jsonData.propertyName for example to get something meaningful out.

Upvotes: 2

Related Questions