C# 使用js发布到api(asp.net核心mvc)
您好,我使用DTO表示单个值(Id)&试图使用ApiController发布到Db,但在单击按钮时,我不断收到错误400,这是指xhr.send错误。 (im使用asp.net core 2.1) 代码: @节脚本{C# 使用js发布到api(asp.net核心mvc),c#,jquery,asp.net,asp.net-mvc,asp.net-core,C#,Jquery,Asp.net,Asp.net Mvc,Asp.net Core,您好,我使用DTO表示单个值(Id)&试图使用ApiController发布到Db,但在单击按钮时,我不断收到错误400,这是指xhr.send错误。 (im使用asp.net core 2.1) 代码: @节脚本{ <script type="text/javascript"> $(document) .ready(function() { $(".js-toggle-HandShake")
<script type="text/javascript">
$(document)
.ready(function() {
$(".js-toggle-HandShake")
.click(function(e) {
var button = $(e.target);
console.log(button.attr("data-QuettaOfferId")); //Value=24 >> OK
$.post("/Api/HandShake/", { QuettaOfferId: button.attr("data-QuettaOfferId") })
// Error in > POST https://localhost:44339/Api/HandShake/ 400 () &
//in jquery>> xhr.send( options.hasContent && options.data || null );
.done(function() {
button
.text("Chousen");
})
.fail(function() {
alert("Something failed");
});
});
});
</script>
试着替换
$.post(“/Api/Hand/”,{QuettaOfferId:button.attr(“data QuettaOfferId”)})
通过
$.post(“/Api/HandShake/”,{QuettaOfferId:button.attr(“data QuettaOfferId”)})
由于您的api控制器名为握手控制器请将
HandShakeDto
类发布到/api/Hand
,但您的控制器名为HandShakeController
,而不是HandController
Opps打字,我已经更新了它,抱歉。如果您想点击该方法,则需要使用/api/握手/握手
@StephenMuecke,我曾经尝试过在api控制器中使用握手>POST 400()更新404()代码,尝试将公共IActionResult握手(握手到dto)
更改为公共IActionResult POST(握手到dto)
或在javascript中尝试更改$。POST(“/Api/HandShake/”,{QuettaOfferId:button.attr(“data QuettaOfferId”)})
to$.post(“/Api/HandShake/Handshakes”),{QuettaOfferId:button.attr(“data QuettaOfferId”)}点击Api控制器中的方法是否正确注册了控制器依赖项?公共握手控制器(ApplicationDbContext上下文,UserManager UserManager/*,IHostingEnvironment environment*/)
感谢您的帮助,但:
[Microsoft.AspNetCore.Mvc.Route("api/[controller]")]
[ApiController]
[Microsoft.AspNetCore.Authorization.Authorize]
public class HandShakeController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityUser> _userManager;
// private readonly IHostingEnvironment hostingEnvironment;
public HandShakeController(ApplicationDbContext context ,UserManager<IdentityUser> userManager/*, IHostingEnvironment environment*/)
{
_context = context;
_userManager = userManager;
//hostingEnvironment = environment;
}
[Microsoft.AspNetCore.Mvc.HttpPost]
// public IHttpActionResult HandShakes(HandShakeDto dto)
public IActionResult HandShakes(HandShakeDto dto)
{
var userId = _userManager.GetUserId(User);
var check = _context.Quetta.Where(u => u.SiteUserId == userId);
if ( _context.handShakes.Any(f => f.QuettaOfferId == dto.QuettaOfferId))
return BadRequest("Some error Msg");
if (check.Any())
{
var hand = new HandShake
{
QuettaOfferId = dto.QuettaOfferId
};
try
{
_context.handShakes.Add(hand);
_context.SaveChangesAsync();
return Ok();
}
catch (Exception e)
{
Console.WriteLine(e);
return BadRequest("Some error Msg");
}
}
else{
return BadRequest("");}
// Check if the user id that publish the ed = login user.
//if so add the offer to selected table,
}
}
public class HandShakeDto
{
public int QuettaOfferId { get; set; }
}