0

aspnetcore API调用上面使用时角为:

function Login(email, password, callback) 
{ 
     var GetAll = new Object(); 
     GetAll.email = email; 
     GetAll.password = email; 
     $http({ 
      url: "http://localhost:52587/api/TokenAuth/Login", 
      dataType: 'json', 
      method: 'post', 
      data: JSON.stringify(GetAll),  
      headers: { 
         'Content-Type': 'application/json' 
         }  
     }) 
     .then(function loginSuccessCallback(response) 
{... 

我测试过这一点使用招摇和Ajax这工作得很好。我在主体中设置了json对象(设置为raw和JSON(application/json))。

我写了下面的Web API:

namespace WEBAPI.Controllers 
{ 
    public class user 
    { 
     public string email { get; set; } 
     public string password { get; set; } 
    } 

[Produces("application/json")] 
[Route("api/[controller]")]  
public class TokenAuthController : Controller  
{ 

    [HttpPost("Login")]  
    public async Task`<IActionResult>` Login([FromBody]user usr) 
{ 
..................    
..................    
}  
}   
} 

但我正在逐渐[FromBody] 415不支持的媒体类型和 无[FromBody招到空参数。请帮我
有人看到我要去哪里错了吗?

回答

0

您可以尝试添加accept

headers: { 
     'Content-Type': 'application/json', 
     'Accept': 'application/json' 
} 
+0

我也试过,但我得到415不支持的媒体类型。 –

2

有没有必要JSON.stringify您的数据,因为你真正需要的是一个JSON对象。将您的数据作为字符串将肯定会为您带来415(不支持的媒体类型)错误。 你可以这样做

$http({ 
      url: "http://localhost:52587/api/TokenAuth/Login", 
      dataType: 'json', 
      method: 'post', 
      data: GetAll,  
      headers: { 
         'Content-Type': 'application/json' 
         }  
     }) 
.then(function loginSuccessCallback(response) 
相关问题