I'm using MVC4
I am using Ajax to return a JSon value
function GetTopConnector(logoId) {
//Get all products for Memory
$.ajax({
type: "POST",
url:"Product/GetJsonTopConnectors",
contentType: "application/json;",
dataType: "json",
success: function(data) { OnSuccess(data); },
failure: OnFailure,
error: OnError
});
};
function OnSuccess(response) {
var obj = JSON.parse(response); // fails, as does JQuery.parse
var company = response[0].company;
var partNumber = response.partNumber;
var thumbnail = response["thumbnail"];
alert(company + " --- " + partNumber + " --- " + thumbnail + " --- ");
}
And my controller is
public JsonResult GetJsonTopClickConnectors()
{
var connector = bll.Connectors.GetTopConnector();
var result = "{'partNumber':'" + connector.PartNumber + "','company':'" + connector.CompanyName + "','thumbnail':'" + connector.Thumbnail + "'}";
return Json(result);
}
The above 'works' in the sense that the OnSuccess js function is called, and the response variable has a value.
According to what I've read, this value is already a Json formatted value. If I have understood correctly, JSon has no special properties, it's just a string in a given formatted. As you can see in my OnSuccess function, I want to treat it like an object.
Google searches suggest I parse it to an object, such as JSON.parse() but this throws an expection and my results are never populated.
The goal is to be able to do var company = response.company
How can I do this?