0

我正在尝试使用 AJAX 将此对象放入 /api/company/#/:

    {"CompanyID":2,"Name":"Test Company","Address1":"","Address2":"","City":"","State":"","Zip":"","ContactName":"","ContactPhone":"","ContactEmail":"","EmployeeCount":"","TypeOfIndustry":"","CompanyRevenue":""}

我的 PUT 方法:

    public void Put (CompanyOverviewView company)
    {
    }

公司概况查看:

public class CompanyOverviewView {
    public int CompanyID { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string ContactName { get; set; }
    public string ContactPhone { get; set; }
    public string ContactEmail { get; set; }
    public string CompanyType { get; set; }
    public Nullable<int> EmployeeCount { get; set; }
    public string TypeOfIndustry { get; set; }
    public Nullable<decimal> CompanyRevenue { get; set; }

AJAX 属性:

var ajaxProperties = {
        type: "PUT",
    url: "/api/company/5/",
    dataType: "json",
    data: JSON.stringify(postdata)
    }

尽管使用默认的 WebAPI 路由,但我收到 404 错误。为什么这不挂钩?我错过了什么吗?

4

2 回答 2

0

尝试将processData: false和添加contentType: 'application/json; charset=utf-8'到您的ajaxProperties. 这将告诉ajax以 JSON 正文而不是 URL 参数的形式发送请求。

于 2013-08-29T20:08:45.197 回答
0

可能是您在不需要时在 URL 中添加了 ID。尝试在 ajax 中仅发布/放置到 /api/company。或者,在您的 Put 方法中尝试将 'string id' 作为另一个参数,看看是否有帮助。

不过,在这种情况下,您可能还需要将 FromBody 或 FromUri 属性放在每个参数上。

于 2013-08-29T21:59:44.303 回答