1

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.

Thiago Loureiro
  • 1,041
  • 13
  • 24
user3021830
  • 2,784
  • 2
  • 22
  • 43
  • 2
    what is the status code returned in your server side httpclient? – Jeric Cruz Sep 12 '17 at 08:05
  • In the client side code try method: 'GET' instead of type – Smac Sep 12 '17 at 08:05
  • It does not make sense but as far as I can see response.statusCode returns function (e){var t;if(e)if(2>b)for(t in e)m[t]=[m[t],e[t]];else C.always(e[C.status]);return this} – user3021830 Sep 12 '17 at 08:08
  • I think I got where I screwed up. My WebAPI and my site is running on different domain and I am getting "XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:55973' is therefore not allowed access." error. I think this is what I need to fix. – user3021830 Sep 12 '17 at 08:12
  • 1
    look for CORS, i had similar issue and enabled cors and worked. – Thiago Loureiro Sep 12 '17 at 21:02

1 Answers1

1

As I have denoted in the update the calls were failed because the domain addresses of my web application and WebAPI were different.

To solve the problem I have used information provided here : Enabling Cross-Origin Requests in ASP.NET Web API 2. And now it works.

user3021830
  • 2,784
  • 2
  • 22
  • 43