【问题标题】:POST works in Postman but not in browserPOST 在 Postman 中有效,但在浏览器中无效
【发布时间】:2018-04-18 11:24:58
【问题描述】:

我在 vuejs 中有这个使用 axios

  axios({
    method: 'post',
    url: 'http://localhost:64427/api/Authenticate/Token',
    headers: {
      'Content-type': 'application/json'
    },
    data: {
      quad: this.quad,
      password: this.password
    }
  })
  .then(response => {
    console.log(response.header.token);
  })

当我在邮递员中使用端点发帖时,它可以工作

http://localhost:64427/api/Authenticate/Token?quad=YGOP&password=P@ssw0rd

但是当我使用 axios 发布时,我可以 404 错误代码。

这里是后台

[HttpPost]
[ActionName("Token")]
[BasicAuthentication]
public HttpResponseMessage Login(string quad, string password)
{
    bool isAuthenticated = EmployeeSecurity.Login(quad, password);
    if(isAuthenticated)
    {
        if (Thread.CurrentPrincipal != null && Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            var basicAuthenticationIdentity = Thread.CurrentPrincipal.Identity;
            if (basicAuthenticationIdentity != null)
            {
                var username = basicAuthenticationIdentity.Name;
                return GetAuthToken(username);
            }
            return null;

        }
    }

    return null;
}

【问题讨论】:

  • 您通过 Postman 访问的 URL 在 URL 参数中有数据,但另一个在正文中有数据。您能否分享您的 API 操作代码以获取更多详细信息?
  • 你似乎通过邮递员使用方法“GET”,通过 axios 使用“POST”
  • @csblo 你可以在 post 方法中使用查询参数
  • @mast3rd3mon 是的,正因为如此,我没有将其作为答案发送:)
  • @csblo 都设置为 POST

标签: asp.net-web-api vue.js vuejs2 axios


【解决方案1】:

您需要更改后端以接受您从客户端发送的模型,您可以通过添加包含 2 个属性的类 LoginData 并将其放入 Login 函数中并使用 FromBody 属性将其映射到正文而不是网址。

        [HttpPost]
        [ActionName("Token")]
        [BasicAuthentication]
        public HttpResponseMessage Login([FromBody] LoginData login)
        {
            bool isAuthenticated = EmployeeSecurity.Login(login.quad, login.password);
            if (isAuthenticated)
            {
                if (Thread.CurrentPrincipal != null && Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    var basicAuthenticationIdentity = Thread.CurrentPrincipal.Identity;
                    if (basicAuthenticationIdentity != null)
                    {
                        var username = basicAuthenticationIdentity.Name;
                        return GetAuthToken(username);
                    }
                    return null;

                }
            }

            return null;
        }

        public class LoginData
        {
            public string quad { get; set; }

            public string password { get; set; }
        }

【讨论】:

    【解决方案2】:

    您的 postman 帖子使用参数传递用户名和密码 ?quad=YGOP&password=P@ssw0rd,而您的 axios 帖子使用有效负载/正文传递数据

    data: {
      quad: this.quad,
      password: this.password
    }
    

    尝试改变这一行:

    url: 'http://localhost:64427/api/Authenticate/Token?quad=' + this.quad + '&password=' + this.password,
    

    然后将data 设置为{}

    【讨论】:

    • 如果您使用 POST,通过 URL 参数发送数据会起作用,但不是一个好习惯
    • 我从来没有说过这是一个好的做法,但我看不到后端/api,这是我能提供的唯一解决方案
    猜你喜欢
    • 2020-07-31
    • 2021-04-03
    • 2016-07-11
    • 2018-11-30
    • 2020-04-28
    • 2021-11-12
    • 2021-09-19
    • 2018-06-30
    • 1970-01-01
    相关资源
    最近更新 更多