简体   繁体   English

无法从WebAPI获得响应

[英]Cannot get response from WebAPI

UPDATE : Now I have better understanding of the error. 更新:现在我对错误有了更好的了解。 I am facing the error explained here : https://stackoverflow.com/a/20035319/3021830 . 我遇到的错误在这里解释: https : //stackoverflow.com/a/20035319/3021830 This is now what I should fix. 现在这是我应该解决的问题。

I have REST API created with ASP.NET WebAPI. 我有使用ASP.NET WebAPI创建的REST API。 It is hosted on http://localhost:54700/ . 它托管在http:// localhost:54700 /上 I also have a GET method with route "api/RefData", so basically I need to call http://localhost:54700/api/RefData . 我还有一个路由为“ api / RefData”的GET方法,因此基本上我需要调用http:// localhost:54700 / api / RefData IT is also marked with AllowAnonymous attribute IT也标记有AllowAnonymous属性

When I make a GET call from PostMan everything seems to work fine. 当我从PostMan拨打GET电话时,一切似乎都正常。 But I try to call it from my ASP.NET MVC application, both from server and client I can not get any result. 但是我尝试从服务器和客户端的ASP.NET MVC应用程序中调用它,但无法获得任何结果。

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. 我用try-catch块包装了代码,但它也没有引发异常。

The client-side code falls to error with statusText "error". 客户端代码因statusText“错误”而出错。

What is wrong with these codes and how may I fix? 这些代码有什么问题,我该如何解决?

Thanks in advance. 提前致谢。

As I have denoted in the update the calls were failed because the domain addresses of my web application and WebAPI were different. 正如我在更新中所述,由于我的Web应用程序和WebAPI的域地址不同,因此调用失败。

To solve the problem I have used information provided here : Enabling Cross-Origin Requests in ASP.NET Web API 2 . 为了解决该问题,我使用了此处提供的信息: 在ASP.NET Web API 2中启用跨域请求 And now it works. 现在就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM