Reputation:
I am using jQuery
.load()
function to load the results from server and append it in my html:
$("#content").load('Get/Information?class=Arts&min=1',
function (responseText, textStatus, XMLHttpRequest) {
console.log(responseText);
// parseJSON(responseText);
});
In my controller I tried converting my jagged array from the model into a json object and then send it to the client:
public JsonResult GetInformation(string class, int min){
var stdObj = new Student();
string[][] information = stdObj.GetInformation(class, min);
return Json(information, JsonRequestBehavior.AllowGet);
}
And after this when I check the response through console.log(responseText);
I have a jSon like string in the following format:
[
["93","Title-1","http://stackoverflow.com"],
["92"," Title-2","http://stackoverflow.com"],
["90"," Title-3","http://stackoverflow.com"],
["89"," Title-4","http://stackoverflow.com"],
["89"," Title-5","http://stackoverflow.com"],
null,null,null,null,null]
I am not sure if this a proper jSon, as from what I learn jSon is in name: value pairs, but here there are only values, considering this how can I read this string/array and print in following manner:
This just a representation(pseudo code) of how I required it to be handled:
for(int 1=0; i<response.length();i++){
$("#content").html('<h1>'+i.Id+'</h1>'+'<p>'+i.Title+'</p>'+'<span>'+i.url+'</span>')
.slideDown('slow');
// $("#content").html('<h1>39</h1>'+'<p>Title-1</p>'+'<span>http://stackoverflow.com</span>');
}
Upvotes: 1
Views: 50725
Reputation: 2914
If your response array is
myArray =
[
["93","Title-1","http://stackoverflow.com"],
["92"," Title-2","http://stackoverflow.com"],
["90"," Title-3","http://stackoverflow.com"],
["89"," Title-4","http://stackoverflow.com"],
["89"," Title-5","http://stackoverflow.com"],
]
you can use
for(int i=0; i<myArray.length();i++)
{
$("#content").html('<h1>'+myArray[i][0]+'</h1>'+'<p>'+myArray[i][1]+'</p>'+'<span>'+myArray[i][2]+'</span>').slideDown('slow');
}
Demo: http://jsfiddle.net/KX596/1/
Upvotes: 1
Reputation: 9167
$.getJSON('Get/Information', { class: "Arts", min: 1 })
.done(function( json ) {
$.each(json , function(i, v) {
var id = v[0]; // etc
});
});
Refer to getJSON
Upvotes: 0
Reputation: 388436
You need to use getJSON()
$.getJSON('Get/Information?class=Arts&min=1', function (response) {
$.each(response, function(idx, rec){
$("#content").append('<h1>'+rec.Id+'</h1>'+'<p>'rec.Title+'</p>'+'<span>'+rec.url+'</span>').slideDown('slow');
})
});
Upvotes: 0