0

i'm currently learning asp.net core MVC and when i tried to post data using jquery ajax i got this error

Failed to load resource: the server responded with a status of 400 ()

here is my controller :

[HttpPost]
    public async Task<IActionResult> PostOrders([FromBody] OrderDetailModel model)
    {
        try
        {
            foreach (var ord in model.Items)
            {
                var orderDetail = new OrderDetail()
                {
                    ItemId = ord.ItemId,
                    Quantity = ord.Quantity
                };

                _db.OrderDetails.Add(orderDetail);
            }

            await _db.SaveChangesAsync();
            return Json(new { success = true, message = "Orders created successfully" });
        }
        catch (Exception ex)
        {
            return BadRequest(new { message = ex.Message });
        }
    }

My jquery :

var itemId = $("#itemDropdown").val();
            var itemName = $("#itemDropdown option:selected").text();
            var quantity = $("#quantity").val();

            var selectedItem = {
                itemId: parseInt(itemId),
                itemName: itemName,
                quantity: parseInt(quantity)
            };

$("#submitButton").click(function () {
            event.preventDefault();

            var data = {
                items : selectedItems
            }

            
            console.log(data);

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Orders/PostOrders",
                processData: true,
                cache: false,
                dataType: "json",
                data: JSON.stringify(data),
                error: function (jqXHR) {
                    console.log(jqXHR);
                }
            });
        })

i tried debugging it, the error was in the line

await _db.SaveChangesAsync();

when it triggers the catch badrequest, any idea why it throws an error? is it because of the await ?

Thanks in advance!

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • What's the *actual* error caused by `SaveChanges`? You're hiding this in your try-catch `return BadRequest` - so the ajax will always see a 400 because that's what you're returning. Try replacing this with just `throw` (or don't catch) and see what error you get on the 500. – freedomn-m Sep 16 '20 at 05:27
  • This may be your solution: https://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages – freedomn-m Sep 16 '20 at 05:27
  • Forgot about jQuery and ajax stuff for a moment. Have you traced into _db.SaveChangesAsync() ? – Jamal Sep 16 '20 at 05:27
  • 1
    @freedomn-m yeah you're right, the ajax will always sees a 400 because of what i'm returning, later i found out that the problem is from my database, thanks for your comment! i appreciate it – Hosse Fernando Sep 16 '20 at 08:25
  • Hi @Jamal, yes i'm still learning haha but thanks for the comment tho, appreciate it! – Hosse Fernando Sep 16 '20 at 08:25

0 Answers0