1

I have my MVC project running under a sub domain, say json.mysite.com and my controller being something similar to

/// assume this method is in some controller - MyJsonController.cs
public JsonResult GetStoreDetails(int storeId)
{
    .....
    return Json(MyStatisticsObject, JsonRequestBehavior.AllowGet);
}

This works fine, only as long as I load any page from the same sub domain json.mysite.local. All I need to do is

    $.ajax({
    url: "/MyJson/GetStoreDetails?storeId=18"
    success: function (result) {
    ...
    }
});

say, in firebug and I can see the json returned beautifully.

Now, I go back to my main asp.net project that runs on different subdomain (technically, they are completely different projects), I am doing this to grab json off my json domain

try
{
    string jsonUrl = string.format("http://json.mysite.local/MyJson/GetStoreDetails?storeId={0}", SelectedStore.Id);

    WebRequest requestObj = WebRequest.Create(jsonUrl);
    requestObj.Method = "GET"; //this did not do anything
    WebResponse responseObj = requestObj.GetResponse();

    string json;
    using (StreamReader objStreamReader = new StreamReader(responseObj.GetResponseStream()))
    {
        json = objStreamReader.ReadToEnd();
    }

    JavaScriptSerializer jss = new JavaScriptSerializer();
    DataTransfer.MyStoreStats storeStats = jss.Deserialize<DataTransfer.MyStoreStats>(json); //this line ALWAYS throws exception
}
catch (Exception ex)
{
    Log.Fatal("storeId: " + SelectedStore.Id, ex);
    base.RaiseError(ErrorCode._500);
}

I always get an exception. If I inspect the json string, I always have a whole lotta full document from <html> to closing tag, and I don't even have a view to this controller.

I tried several things like changing post type etc but nothing seems to be working. What needs to be done if I need to get just my json here please?

Thanks much.

LocustHorde
  • 6,361
  • 16
  • 65
  • 94
  • Try to use `httpWebRequest.Accept = "application/json";` – Codo Sep 20 '11 at 17:57
  • Check out [this article](http://geekswithblogs.net/JuanDoNeblo/archive/2007/10/24/json_in_aspnetajax_part2.aspx) for an example of how to call a remote service using JSON. – counsellorben Sep 20 '11 at 17:25
  • Hi, I am doing almost exactly the same thing.. I tried to debug the json method in controller (mvc proj) and ran my asp.net page, the breakpoint was never hit! (in my mvc) so I am not sure if the method is being called?! :-s – LocustHorde Sep 20 '11 at 17:43
  • You are invoking the abstract class `WebRequest`, while the article I linked to used the class `HttpWebRequest`, which is an HTTP specific implementation of Webrequest. I suggest you switch to use the HttpWebRequest class. – counsellorben Sep 20 '11 at 18:21
  • Hi, I did that too, but the result is still the same – LocustHorde Sep 21 '11 at 08:06
  • Please give details on the exception. Also, please give details on the HTML page you get back. – counsellorben Sep 21 '11 at 10:43
  • Hi, I found the error, I had a base controller that was authentication and I wasn't passing the authentication details.. Now I am confused whether I should delete this question or make your answer as right answer! – LocustHorde Sep 21 '11 at 17:33
  • Use your best judgment. However, did you find the error as a result of my request that you check the details of the HTML page returned? If so, then I deserve some credit. – counsellorben Sep 21 '11 at 17:38
  • Yes, I did only look at returning html after your above comment, and realized that it was returning my login page.. so your second comment saved the day, thanks very much! – LocustHorde Sep 22 '11 at 15:20
  • You're welcome. Best of luck. – counsellorben Sep 22 '11 at 15:30

0 Answers0