如果启用 AJAX 的 WCF 应返回一个对象,其日期属性为 Nothing,则对该服务的请求将失败。
测试.svc
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports System.Runtime.Serialization
<ServiceContract(Namespace:="WebApp")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class Test
<OperationContract()>
<WebGet()>
Public Function TestOperation() As CompositeType
Dim obj As New CompositeType
obj.DateProp = Nothing
obj.StringProp = "Test"
Return obj
End Function
End Class
<DataContract()>
Public Class CompositeType
<DataMember()>
Public Property DateProp() As Date
<DataMember()>
Public Property StringProp() As String
End Class
Testform.aspx 上的脚本标签
<script type="text/javascript">
$(document).ready(function () {
$.getJSON('./Test.svc/TestOperation', function (data) {
alert(data.d.DateProp);
});
});
</script>
Web.config(服务模型部分)
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebApp.TestAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="WebApp.Test">
<endpoint address="" behaviorConfiguration="WebApp.TestAspNetAjaxBehavior"
binding="webHttpBinding" contract="WebApp.Test" />
</service>
</services>
</system.serviceModel>
Chrome 在此调用中显示以下错误消息:“加载资源失败”。
有没有人对这种行为有解释/想法,你应该如何防止这个问题?
谢谢你的建议 ;)