我用axios在vuejs中找到了这个
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);
})当我在postman中使用端点时,它可以工作。
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;
}发布于 2018-04-19 20:26:10
您需要更改后端以接受从客户端发送的模型,可以通过添加包含2个属性的类LoginData并将其放入Login函数中,并使用FromBody属性从正文而不是url映射它。
[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; }
}发布于 2018-04-18 11:28:54
邮递员帖子使用参数传递用户名和密码?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设置为{}
https://stackoverflow.com/questions/49898530
复制相似问题