0

I am trying to access a REST Service in my MVC application.I am calling getJSON method to get the data from a controller which internally calls the REST service which returns data in json format.But I am getting the a lot of "\ in my output of DownLoadString method and my return Json is not returning proper JSON data and hence my client side script is not able to access the JSON properties.

My Script in my view is

$.getJSON("@Url.Action("GetManufacturers", "Home")",function(data){
     console.debug("Status is : "+data.Status)
});

My Action method looks like this

public ActionResult GetManufacturers()
{          
        string restURL ="http://mytestserver/myapi/Manufacturers";
        using (var client = new WebClient())
        {               
            var data = client.DownloadString(restURL); 

           //data variable gets "\" everywhere
            return Json(data,JsonRequestBehavior.AllowGet);                      
        }   
}

I used visual studio breakpoints in my action method and i am seeing a lot of \" enter image description here

And i checked what is coming out to my getJSON callback and the JSON tab is empty. enter image description here But my response tab has content like this

enter image description here

I belive if there is no \", i would be able to parse it nicely.

I used fiddler to see whether i am getting correct (JSON format) data from my REST service and it seems fine.

enter image description here

Can anyone help me to tackle this ? I would like to return proper JSON from my action method. Sometime i may want to read the json properties in the C# code itself. I saw some example of doing it with DataContractJsonSerializer. But that needs a concrete type to be converted to. I don't want to do that. because other clients would also access my RESTService and how will expect them to write a fake entity for this ?

Happy
  • 1,767
  • 6
  • 22
  • 26

1 Answers1

1

You need to return the data as is:

public ActionResult GetManufacturers()
{          
    string restURL ="http://mytestserver/myapi/Manufacturers";
    using (var client = new WebClient())
    {               
        var data = client.DownloadString(restURL); 
        return Content(data, "application/json");                      
    }   
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin. I tried that with jquery post at client side and i am getting the same result in my firebub. Nothing in JSON tab and same content in respons tab (as of the screenshots in my question). – Happy May 30 '12 at 14:46