I'm trying to pass information between domains using a combination of jQuery and WCF. This works really well for passing simple data types. This is demonstrated by the TestSimple Method below. However when I use the TestComplex method the WCF method receives nothing as the address parameter.
Hopefully an example will make my question clearer:
WCF Service:
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
Client Script
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); })
}
Data Contract:
<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
The Request for the TextComplex method
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
The Response for the TextComplex method
jQuery15107153733184290069_1329385242312({"__type":"BasicAddress:#WPC.FADS.Web","Building":"Test","County":"Test","Latitude":0,"Locality":"Test","Longitude":0,"Postcode":"Nothing","Street":"Test","Town":"Test"});
TestSimple works but TestComplex does not, Is what I'm trying to achieve with TextComplex possible?
EDIT
also tried without the __type parameter as suggested by Zach:
var data = {
Address: {
"Building": "Test",
"County": "Test",
"Latitude": 0,
"Locality": "Test",
"Longitude": 0,
"Postcode": "test",
"Street": "Test",
"Town": "Test"
}
}
Operation contracts:
<OperationContract()>
<WebGet(ResponseFormat:=WebMessageFormat.Json)>
Function TestSimple(postcode As String) As BasicAddress
<OperationContract()>
<WebGet(ResponseFormat:=WebMessageFormat.Json)>
Function TestComplex(Address As BasicAddress) As BasicAddress