【问题标题】:how to post to webapi in ionic?如何在 ionic 中发布到 web api?
【发布时间】:2017-06-20 11:23:55
【问题描述】:

当我运行我的应用程序并在 chrome 中发布数据时,它显示以下错误:POST http://10.10.9.169/UserService/api/account 401 (Unauthorized)

我已经安装了 cors 并在 webapiconfig.cs 文件中启用了它,我还在 global.asax 文件中添加了以下内容:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}

这是我的 controllers.js 代码:

.controller('SignUp15Ctrl', ['$scope', '$stateParams', '$location','userFactoryReg', function ($scope, $stateParams, $location, userFactoryReg) {
    $scope.userdataReg = {}
    $scope.enterloginReg = function(usern,pass1,pass2) {
        if(pass1!=pass2)
        {
            alert('Passwords do not match.');
        }
        if(pass1 == pass2)
        {
            userFactoryReg.postUser(usern,pass1,pass2).then(function(response)
            {
                if(JSON.stringify(response.data) === "null")
                {
                    alert('Error');
                }
                else
                {
                    alert('Accoiunt successfully created.');
                    $location.path('/page4');
                }
            });
        }
    }
}])

这是我的 services.js 代码:

.factory('userFactoryReg', ['$http', function($http) {
    var users = [];
    return {
        postUser: function(uname,passw1,passw2){
            return $http.post("http://10.10.9.169/UserService/api/account/register", {"Username" :uname, "Password" :passw1, "ConfirmPassword" :passw2} );
        }
    }
}])

这是我的 webapi 部分代码:

[AllowAnonymous]
public HttpResponseMessage Post([FromBody] User user)
{
    try
    {
        using (UsersDBEntities entities = new UsersDBEntities())
        {
            entities.Users.Add(user);
            entities.SaveChanges();

            var message = Request.CreateResponse(HttpStatusCode.Created, user);
            message.Headers.Location = new Uri(Request.RequestUri + user.Username.ToString());

            return message;
        }
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
    }
}

【问题讨论】:

  • 如果您需要全局身份验证,您是否在登录操作中添加了[AllowAnonymous]
  • 我尝试添加 [allowanonymous] 但它返回:POST 10.10.9.169/UserService/api/account 400 (Bad Request)
  • 您能否将该 API 操作的代码添加到您的问题中?
  • 已添加。不知道我是否做对了。
  • 如果遇到异常,您的操作似乎会返回 400,您可以在此处设置断点并查看它是什么吗?

标签: angularjs asp.net-web-api ionic-framework


【解决方案1】:

第一个问题是由于 API 操作上没有 [AllowAnonymous] 并且需要在全局或控制器级别使用 [Authorize] 进行身份验证。您必须允许未经身份验证的用户访问身份验证端点。

第二个问题是返回 HTTP 400,但这是由于 User 实体中的验证错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2015-10-02
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多