jQuery posts null instead of JSON to ASP.NET Web API

后端 未结 5 752
南笙
南笙 2020-12-05 19:12

I can\'t seem to get this to work... I have some jQuery like this on the client:

$.ajax({
    type: \"POST\",
    url: \"api/report/reportexists/\",
    data         


        
5条回答
  •  北海茫月
    2020-12-05 20:05

    This worked for me, all other approaches didn't:

    function addProduct() {
            var product = { 'Id': 12, 'Name': 'Maya', 'Category': 'newcat', 'Price': 1234 };             
            $.ajax({
                type: "POST",
                url: "../api/products",
                async: true,
                cache: false,
                type: 'POST',
                data: product,
                dataType: "json",
                 success: function (result) {
    
                },
                error: function (jqXHR, exception) {
                    alert(exception);
                }
            });
        }
    

    Serverside:

     [HttpPost]
        public Product[] AddNewProduct([FromBody]Product prod)
        {
            new List(products).Add(prod);
            return products;
        }
    

提交回复
热议问题