weixin_33736649 2017-03-07 09:09 采纳率: 0%
浏览 14

具有路由控制器的HttpPut

Hi i need some help I'm trying to send some data with ajax to a controller using PUT Method to do update here is the javascript and ajax code:

function UpdateProduct() {
var id = location.pathname.split('/')[2];

var product = {
    Name: $("#txtName").val(),
    Description: $("#txtDescription").val(),
    Price: $("#txtPrice").val(),
    SubCategoryId: $("#txtSubCategoryId").val(),
    CompanyId: $("#txtSubCategoryId").val(
    ProductCode: $("#ProductCode").val(
    Barcode: $("#Barcode").val(
    Brand: $("#txtBrand").val(),
    Material: $("#txtMaterial").val(),
    Date: $("#txtDate").val(),
};

$.ajax({
    url: "/UpdateProduct/"+id,
    type: "PUT",
    dataType: "json",
    data: product,
    success: function (result) {
        alert('Successful');
    },
    error: function (e) {
        alert(e.error);
    }
});

};

and my controller code:

[HttpPut]
    [Route("UpdateProduct/{id}")]
    public void UpdateProduct(int id, ProductsBE product)
    {
        var api = ConfigurationManager.AppSettings["apiUrl"];
        var productsById = new ProductsRep();
        var searchResponse = productsById.UpdateProduct(product,id, api);
    }

The API is working because i have tested it with postman.

  • 写回答

1条回答 默认 最新

  • csdnceshi62 2017-03-07 10:53
    关注

    you have to use JSON.stringify to convert your object product to json string

    $.ajax({
        url: "/UpdateProduct/"+id,
        type: "PUT",
        dataType: "json",
        data: JSON.stringify(product),
        success: function (result) {
            alert('Successful');
        },
        error: function (e) {
            alert(e.error);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题