用js发布到api(asp.net core mvc)

编程入门 行业动态 更新时间:2024-10-24 04:45:49
本文介绍了用js发布到api(asp core mvc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我将DTO用于单个值(Id)&尝试使用ApiController发布到Db,但是在单击按钮时,我不断收到错误400,该错误将我引向xhr.send错误. (即使用asp core 2.1) 代码 : @section脚本{

Hello im using a DTO for a single value(Id) & trying to post to Db using ApiController but on button click I keep getting error 400 that is referring me to xhr.send error. (im using asp core 2.1 ) Code : @section Scripts{

<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 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>

} & ApiController代码

} & the ApiController code

[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, } }

im使用asp core 2.1&强烈怀疑问题出在ApiController中,但我不确定.

im using asp core 2.1 & strongly suspect that the problem is in the ApiController but im not sure.

DTO

public class HandShakeDto { public int QuettaOfferId { get; set; } }

推荐答案

尝试替换 $.post("/Api/Hand/", { QuettaOfferId: button.attr("data-QuettaOfferId") }) 经过 $.post("/Api/HandShake/", { QuettaOfferId: button.attr("data-QuettaOfferId") })

Try replacing $.post("/Api/Hand/", { QuettaOfferId: button.attr("data-QuettaOfferId") }) By $.post("/Api/HandShake/", { QuettaOfferId: button.attr("data-QuettaOfferId") })

因为您的api控制器名称是HandShakeController

as your api controller name is HandShakeController

更多推荐

用js发布到api(asp.net core mvc)

本文发布于:2023-11-16 09:38:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:asp   api   js   mvc   core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!