【发布时间】:2017-05-04 16:22:05
【问题描述】:
当我将 Javascript 日期类型作为参数发送到 WCF 方法时,会出现以下序列化问题。 (他们都在使用 JSON)。
DateTime content '2017-05-04T13:09:11.737Z' does not start with '\/Date(' and end with ')\/' as required for JSON.'.
解决此序列化问题的最佳方法是什么?似乎它可以通过字符串操作来解决,但是对于包含DateTime 类型的所有 WCF 方法,是否有任何通用方法? (客户端或服务器端)
这是测试 Angular Http 帖子
this.http.post(myTestDateUrl, JSON.stringify( {date : new Date()} ) , {headers: this.getHeaders()})
.subscribe(
data => {
console.log(data);
},
error => {
console.log(error);
}
);
private getHeaders() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return headers;
}
这是 WCF 测试方法
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
public Stream TestDate(DateTime date)
{
try
{
dynamic result = new { status = "OK"};
return Send(result);
}
catch (Exception ex)
{
dynamic result = new { status = "ERROR", error = ex.Message };
return Send(result);
}
}
【问题讨论】:
-
您可能会发现这很有用:stackoverflow.com/questions/10286204/…
标签: javascript json angular wcf typescript