4

I,m updating a record using PUT in web api, when I use contentType: 'application/json; charset=utf-8', then my data is not passed to api controller but when i comment this line data is transfered. can any one explain this ? below is my Call from mvc view

$(function () {
        $("#btnSubmit").click(function () {
            var id = $("#hdnProductID").val();
            var ProductName = $("#txtProductName").val();
            var QuantityPerUnit = $("#txtQuantityPerUnit").val();
            var ReorderLevel = $("#txtReorderLevel").val();
            var UnitPrice = $("#txtUnitPrice").val();
            var UnitsInStock = $("#txtUnitsInStock").val();
            var UnitsOnOrder = $("#txtUnitsOnOrder").val();

            $.ajax({
                url: "http://localhost:2821/api/Products"+ "/" + id,
                type: 'PUT',
                contentType: 'application/json; charset=utf-8',
                data:{ProductName:ProductName,QuantityPerUnit:QuantityPerUnit,ReorderLevel:ReorderLevel,UnitPrice:UnitPrice,UnitsInStock:UnitsInStock,UnitsOnOrder:UnitsOnOrder},
                success: function (data) {
                    alert("success");
                },
                error: function (msg) {
                    alert(msg);
                }

            });
        });
    });

Below is my controller method

public IHttpActionResult PutProduct(int id, Product product)
 {}
Muzammil
  • 117
  • 2
  • 9
  • try `data:JSON.stringify({ProductName:ProductName,QuantityPerUnit:QuantityPerUnit,ReorderLevel:ReorderLevel,UnitPrice:UnitPrice,UnitsInStock:UnitsInStock,UnitsOnOrder:UnitsOnOrder})` – T J Nov 05 '15 at 05:55
  • @Muzammil Is `contentType: 'application/json; charset=utf-8',` valid? Have you tried just `contentType: 'application/json;`? See this: http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type – zer00ne Nov 05 '15 at 06:06
  • 1
    @zer00ne the charset is not needed because the only valid one with ajax requestes is utf-8, but it should not be a problem. The problem is that the OP tells the server that JSON is sended but the OP does not pass any JSON formatted data with `data`. – t.niese Nov 05 '15 at 06:09

1 Answers1

2

If contentType is not specified in the request, it takes default contentType, i.e., "application/x-www-form-urlencoded; charset=UTF-8" and no need to stringfy the post data but if contentType is "application/json; charset=utf-8", need to stringfy post data explicitly. So it should be:

    $.ajax({
            url: "http://localhost:2821/api/Products"+ "/" + id,
            type: 'PUT',
            contentType: 'application/json; charset=utf-8',
            data:JSON.stringify({ProductName:ProductName,QuantityPerUnit:QuantityPerUnit,ReorderLevel:ReorderLevel,UnitPrice:UnitPrice,UnitsInStock:UnitsInStock,UnitsOnOrder:UnitsOnOrder}),
            success: function (data) {
                alert("success");
            },
            error: function (msg) {
                alert(msg);
            }

        });
Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18