开发者

MVC Json controller returning full page when called from different project in different sub domain?

开发者 https://www.devze.com 2023-04-06 09:36 出处:网络
I have my MVC project running under a sub domain, say json.mysite.com and my controller being something similar to

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)
{开发者_JAVA技巧
    .....
    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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号