UPDATE : Now I have better understanding of the error. I am facing the error explained here : https://stackoverflow.com/a/20035319/3021830. This is now what I should fix.
I have REST API created with ASP.NET WebAPI. It is hosted on http://localhost:54700/. I also have a GET method with route "api/RefData", so basically I need to call http://localhost:54700/api/RefData. IT is also marked with AllowAnonymous
attribute
When I make a GET call from PostMan everything seems to work fine. But I try to call it from my ASP.NET MVC application, both from server and client I can not get any result.
My server side code is :
private static HttpClient _client;
private static HttpClient Client
{
get
{
if (_client == null)
{
_client = new HttpClient();
_client.BaseAddress = new Uri("http://localhost:54700/");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
return _client;
}
}
internal List<HeardAbout> HeardAbouts
{
get
{
if (System.Web.HttpContext.Current.Session["refData"] == null)
{
GetRefData().Wait();
}
return System.Web.HttpContext.Current.Session["refData"] as List<RefData>;
}
}
private async Task<List<RefData>> GetRefData()
{
try
{
List<RefData> has = new List<RefData>();
// The following line is where the code leaves debugging
HttpResponseMessage response = await Client.GetAsync("api/RefData");
if (response.IsSuccessStatusCode)
{
has = await response.Content.ReadAsAsync<List<RefData>>();
System.Web.HttpContext.Current.Session["refData"] = has;
}
return has;
}
catch (Exception ex)
{
throw;
}
}
and my client side code is:
$.ajax({
type: "GET",
url: "http://localhost:54700/api/RefData",
cache: false,
contentType: "application/json; charset=utf-8",
success: function (response) {
if (callback)
callback(response.d);
},
error: function (response) {
if (callback)
error(response.d);
},
});
The server-side codes reaches the commented line. Then leaves debugging. I wrapped the code with try-catch
block but it does not raise an exception as well.
The client-side code falls to error with statusText "error".
What is wrong with these codes and how may I fix?
Thanks in advance.