是否可以使用 jQuery 方法 getJSON 跨域将复杂对象传递给 WCF 服务?
Posted
技术标签:
【中文标题】是否可以使用 jQuery 方法 getJSON 跨域将复杂对象传递给 WCF 服务?【英文标题】:Is it possible to pass complex objects across domains using the jQuery Method getJSON to a WCF Service? 【发布时间】:2012-03-07 17:48:26 【问题描述】:我正在尝试结合使用 jQuery 和 WCF 在域之间传递信息。这对于传递简单的数据类型非常有效。下面的 TestSimple 方法证明了这一点。但是,当我使用 TestComplex 方法时,WCF 方法不会接收任何地址参数。
希望一个例子能让我的问题更清楚:
WCF 服务:
Function TestSimple(postcode As String) As BasicAddress Implements ISubmitVehicle.TestSimple
Return Test(postcode)
End Function
Function TestComplex(Address As BasicAddress) As BasicAddress Implements ISubmitVehicle.TestComplex
If Address Is Nothing Then
Return Test("Nothing")
Else
Return Test(Address.Postcode)
End If
End Function
Function Test(postcode As String) As BasicAddress
Return New BasicAddress With
.Building = "Test", .County = "Test", .Latitude = 0, .Locality = "Test",
.Longitude = 0, .Street = "Test", .Town = "Test", .Postcode = postcode
End Function
客户端脚本
function TestSimple()
var data = postcode: "test" ;
Test(data, 'TestSimple');
function TestComplex()
// I also tried
//var data = Address: Postcode: 'test'
var data =
Address: "__type": "BasicAddress:#WPC.FADS.Web",
"Building": "Test",
"County": "Test",
"Latitude": 0,
"Locality": "Test",
"Longitude": 0,
"Postcode": "test",
"Street": "Test",
"Town": "Test"
Test(data, 'TestComplex');
function Test(data, method)
$.getJSON(baseURL + 'Services/SubmitVehicle.svc/' + method + '?callback=?', data,
function (NewBasicAddress) alert(NewBasicAddress.Postcode); )
数据合同:
<DataContract()>
Public Class BasicAddress
<DataMember()> Property Building As String
<DataMember()> Property Street As String
<DataMember()> Property Locality As String
<DataMember()> Property Town As String
<DataMember()> Property County As String
<DataMember()> Property Postcode As String
<DataMember()> Property Latitude As Double
<DataMember()> Property Longitude As Double
End Class
TextComplex 方法的请求
GET /Services/SubmitVehicle.svc/TestComplex?callback=jQuery15107153733184290069_1329385242312&Address%5B__type%5D=BasicAddress%3A%23WPC.FADS.Web&Address%5BBuilding%5D=Test&Address%5BCounty%5D=Test&Address%5BLatitude%5D=0&Address%5BLocality%5D=Test&Address%5BLongitude%5D=0&Address%5BPostcode%5D=test&Address%5BStreet%5D=Test&Address%5BTown%5D=Test&_=1329385253384 HTTP/1.1
TextComplex 方法的响应
jQuery15107153733184290069_1329385242312("__type":"BasicAddress:#WPC.FADS.Web","Building":"Test","County":"Test","Latitude":0,"Locality":"Test","Longitude":0,"Postcode":"Nothing","Street":"Test","Town":"Test");
TestSimple 有效,但 TestComplex 无效,我尝试使用 TextComplex 实现的目标是否可行?
编辑
也尝试过不使用 Zach 建议的 __type 参数:
var data =
Address:
"Building": "Test",
"County": "Test",
"Latitude": 0,
"Locality": "Test",
"Longitude": 0,
"Postcode": "test",
"Street": "Test",
"Town": "Test"
运营合同:
<OperationContract()>
<WebGet(ResponseFormat:=WebMessageFormat.Json)>
Function TestSimple(postcode As String) As BasicAddress
<OperationContract()>
<WebGet(ResponseFormat:=WebMessageFormat.Json)>
Function TestComplex(Address As BasicAddress) As BasicAddress
【问题讨论】:
【参考方案1】:经过一些研究,我认为答案是“不”。详情请见REST / SOAP endpoints for a WCF service。
万一其他人遇到这个问题,我最终所做的是为我公开的每个方法创建了两个操作合同:
可以使用跨域客户端脚本中的“GetJSON”来使用,其中仅包含简单参数。
一个包含复杂对象作为参数但不能从跨域客户端脚本中使用。
实现操作合约的方法都调用相同的代码。
【讨论】:
以上是关于是否可以使用 jQuery 方法 getJSON 跨域将复杂对象传递给 WCF 服务?的主要内容,如果未能解决你的问题,请参考以下文章
使用 jQuery $.getJSON() 从 Vuejs 方法中的本地 json 文件获取数据