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 matlab处理脑电数据悬赏(时序图+预处理+频谱图)
    • ¥100 r语言多元回归模型怎么看表达式
    • ¥15 PMM 9010/30P
    • ¥15 pom文件依赖管理,未找到依赖
    • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
    • ¥15 Unity导出2D项目运行时图片变成马赛克
    • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
    • ¥15 matlab中此类型的变量不支持使用点进行索引
    • ¥15 咨询第六届工业互联网数据创新大赛原始数据
    • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!