我是Rest服务的新手。我使用Post方法传递Json对象,但是我无法获得类属性的值。
我的代码如下
function SaveBook() {
var bookData = { "ISDCode": "TestISD",
"retailerCode":"RT148",
"count":"2",
"salesdata":[
{
"Serial":"3544334444",
"CustomerPhone":"98234234234",
"CustomerName":"Name1",
"CustomerInfoID":"1",
"TimeStamp":"22-May-14",
"Latitude":"10.3456",
"Longitude":"8.3453"
},
{
"Serial":"35324432324",
"CustomerPhone":"9824322333333",
"CustomerName":"Name2",
"CustomerInfoID":"2",
"TimeStamp":"21-May-14",
"Latitude":"10.3344",
"Longitude":"8.9564"
}]
}
$.ajax({
type: "POST",
url: "http://localhost/API/Service.svc/PostTertiarySales",
data: JSON.stringify(bookData),
contentType: "application/json",
dataType: "json",
//processData: true,
success: function (data, status, jqXHR) {
alert("success..." + data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
</script>
在IService.cs[DataContract]公共类TertiarySales{[DataMember]公共字符串ISDCode{Get;Set;}中
[DataMember]
public string retailerCode { get; set; }
[DataMember]
public string count { get; set; }
[DataMember]
public List<WCFlib.CompositeType.TertiaryData> salesdata { get; set; }
}
[DataContract]
public class TertiaryData
{
public string Serial { get; set; }
public string CustomerPhone { get; set; }
public string CustomerName { get; set; }
public Int32 CustomerInfoID { get; set; }
public DateTime TimeStamp { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public TertiaryData(string Serial, string CustomerPhone, string CustomerName, Int32 CustomerInfoID, DateTime TimeStamp, double Latitude, double Longitude)
{
this.Serial = Serial;
this.CustomerPhone = CustomerPhone;
this.CustomerName = CustomerName;
this.CustomerInfoID = CustomerInfoID;
this.TimeStamp = TimeStamp;
this.Latitude = Latitude;
this.Longitude = Longitude;
}
}
[OperationContract]
[WebInvoke(UriTemplate = "PostTertiarySales",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = "POST")]
bool PostTertiarySales(WCFlib.CompositeType.TertiarySales data);
在服务中。cs
public bool PostTertiarySales(WCFlib.CompositeType.tertiarySales数据){
return true;
}
谢谢
还修饰TertiaryData类属性的
[DataContract]
public class TertiaryData
{
[DataMember]
public string Serial { get; set; }
...
}