0

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?

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • Your JavaScript makes a POST request and says that the POST body is a JSON document, but you have no `data` parameter so there isn't a POST body. – Quentin Jan 11 '15 at 10:00
  • Your controller should be returning an `object`, not a `string` - `return Json(connector);` –  Jan 11 '15 at 10:02

1 Answers1

1
var result = "{'partNumber':'" + connector.PartNumber + "','company':'" + connector.CompanyName + "','thumbnail':'" + connector.Thumbnail + "'}";

That cannot generate valid JSON (' isn't a string delimiter in JSON). Test the output with a linter and find a C# library for generating JSON instead of doing it by mashing strings together.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • As simple as that! Thank you, especially for the link! – MyDaftQuestions Jan 11 '15 at 10:13
  • @MyDaftQuestions. MVC aleady has what you need (the `Json` method) - `return Json(connector);` and then in the view, `var company = response.company` –  Jan 11 '15 at 10:17
  • @StephenMuecke, it returns an internal error 500 when I try that though :( And my deadline is tomorrow – MyDaftQuestions Jan 11 '15 at 10:24
  • Does the object returned by `bll.Connectors.GetTopConnector();` contain any circular references (most common cause for that). You can always create an anonymous object `return Json(new { partNumber = connector.PartNumber, company = connector.CompanyName, ... });` –  Jan 11 '15 at 10:28
  • Internal Server Errors are just errors where the error messages aren't reported to the browser. You'll need to look at your server logs to see the error message. – Quentin Jan 11 '15 at 10:43