.NET Web服务获取null对象

时间:2015-06-17 08:28:33

标签: c# .net ajax web-services rest

我试图使用ajax调用和.NET REST Web服务来保存对象,但我在.NET端获得了null对象。请帮我找出问题所在。

C#对象:

[DataContract]
public class Customer
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

网络服务:

        [WebInvoke(UriTemplate = "SaveCustomer", BodyStyle = WebMessageBodyStyle.Wrapped,
           RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
           Method = "POST")]
        [OperationContract]
        bool SaveCustomer(Customer c);

AJAX致电:

var type = "POST";
var url = "/WS/Service.svc/SaveCustomer";
var contentType = "application/json; charset=utf-8";
var dataType = "json";
var data = '{ "c:":{"Id": "1", "Name": "myname"}';
var processData = true;

$.ajax({
    type: type, 
    url: url, 
    data: data, 
    contentType: contentType, 
    dataType: dataType, 
    processdata: processData, 
    success: function (msg) {
        alert(msg.SaveCustomerResult);
    },
    error: function (error) {
        alert('error');
    }
});

使用它我在调试时得到null:

public bool SaveCustomer(Customer c)
{
    return true;
}

1 个答案:

答案 0 :(得分:0)

可能是这一行中的错误

var data = '{ "c:":{"Id": "1", "Name": "myname"}';

应该是

var data = '{ "c":{"Id": "1", "Name": "myname"}';