Json responses format

I need following json response from ajax call:

[{url:"http://localhost:63220/Images/heroAccent.png"},
{url:"http://localhost:63220/Images/heroAccent.png"}]

My ajax call is:

function loadtest() {
    $.ajax({
        type: 'GET',
        url: '/Home/TestMethod',
        async: true,
        dataType:'json',
        cache: false,
        error: function () {
            alert('Errror');
        },
        success: function (data) {
            console.log(data);
        }
    });
}

My controller is:

public JsonResult TestMethod()
    {
        var items = new[] {
                            new {url = "http://localhost:63220/Images/heroAccent.png"}, 
                            new {url = "http://localhost:63220/Images/heroAccent.png"} };

        return Json(items, JsonRequestBehavior.AllowGet);
    }

It gives the response:

    [{"url":"http://localhost:63220/Images/heroAccent.png"},
    {"url":"http://localhost:63220/Images/heroAccent.png"}]

But I need following response. How can I get it?

[{url:"http://localhost:63220/Images/heroAccent.png"},
{url:"http://localhost:63220/Images/heroAccent.png"}]


Source: json

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.