I'm trying to trigger a jQuery AJAX post to my ASP.NET webservice. However, even though the subscribeToSearch method is executed, when I access the strSubscriber.email variable in that method it's empty. Why?
These are the varaible values I'm logging before they get posted:
email: [email protected]
objecttype: 15
provinceid: 7
city: "test"
distance: 0
minprice: 0
maxprice: 0
frequency: 0
rooms: 0
surface: 0
Here's the AJAX call:
$.ajax({
type: "POST",
url: '/api/subscribetosearch',
async: true,
data: JSON.stringify({
str: {
'objecttype': objecttype,
'email': email,
'provinceid': provinceid,
'city': city,
'distance': distance,
'minprice': minprice,
'maxprice': maxprice,
'frequency': frequency,
'rooms': rooms,
'surface': surface
}
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function () {
},
success: function (msg) {
}
});
The webmethod signatures:
Ihouse.vb
<OperationContract()>
<Web.WebInvoke(Method:="POST", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.WrappedRequest,
UriTemplate:="subscribetosearch")>
Function subscribeToSearch(ByVal str As GlobalFunctions.SearchSubscriber) As Stream
house.svc.vb
Public Function subscribeToSearch(ByVal strSubscriber As GlobalFunctions.SearchSubscriber) As Stream Implements Ihouse.subscribeToSearch
LogError("subscribeToSearch hit")
LogError("subscribeToSearch email: " + strSubscriber.email)
End Function
I checked out the headers in Google Chrome dev console and this is the request payload:
Here's the SearchSubscriber class.
Public Class GlobalFunctions
<Runtime.Serialization.DataContract>
Public Class SearchSubscriber
<Runtime.Serialization.DataMember>
Private _objecttype As Integer
Public Property objecttype() As Integer
Get
Return _objecttype
End Get
Set(ByVal value As Integer)
_objecttype = value
End Set
End Property
Private _email As String
Public Property email() As String
Get
Return _email
End Get
Set(ByVal value As String)
_email = value
End Set
End Property
Private _provinceid As Integer
Public Property provinceid() As Integer
Get
Return _provinceid
End Get
Set(ByVal value As Integer)
_provinceid = value
End Set
End Property
Private _city As String
Public Property city() As String
Get
Return _city
End Get
Set(ByVal value As String)
_city = value
End Set
End Property
Private _distance As Integer
Public Property distance() As Integer
Get
Return _distance
End Get
Set(ByVal value As Integer)
_distance = value
End Set
End Property
Private _minprice As Integer
Public Property minprice() As Integer
Get
Return _minprice
End Get
Set(ByVal value As Integer)
_minprice = value
End Set
End Property
Private _maxprice As Integer
Public Property maxprice() As Integer
Get
Return _maxprice
End Get
Set(ByVal value As Integer)
_maxprice = value
End Set
End Property
Private _frequency As Integer
Public Property frequency() As Integer
Get
Return _frequency
End Get
Set(ByVal value As Integer)
_frequency = value
End Set
End Property
Private _rooms As Integer
Public Property rooms() As Integer
Get
Return _rooms
End Get
Set(ByVal value As Integer)
_rooms = value
End Set
End Property
Private _surface As Integer
Public Property surface() As Integer
Get
Return _surface
End Get
Set(ByVal value As Integer)
_surface = value
End Set
End Property
End Class
End Class
UPDATE 1
Followed suggestions by @Nkosi so now I have this:
//construct the object to be posted.
var searchSubscriber = {
objecttype: objecttype,
email: email,
provinceid: provinceid,
city: city,
distance: distance,
minprice: minprice,
maxprice: maxprice,
frequency: frequency,
rooms: rooms,
surface: surface
};
$.ajax({
type: "POST",
url: '/api/subscribetosearch',
async: true,
data: JSON.stringify(searchSubscriber),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function () {
},
success: function (msg) {
}
});
The request payload is now:
The following method on the server is hit, but the strSubscriber object is nothing...
Public Function subscribeToSearch(ByVal strSubscriber As GlobalFunctions.SearchSubscriber) As Stream Implements Ihouse.subscribeToSearch
LogError("subscribeToSearch HIT") <---- this line gets executed
If strSubscriber Is Nothing Then
LogError("subscribeToSearch strSubscriber IS NOTHING") <---- this line gets executed
Else
LogError("subscribeToSearch strSubscriber IS SOMETHING")
End If
End Function


/api/subscribetosearchis a valid URL forsubscribeToSearchmethod. Am I missing something?.svcextension is visible in the URL.<Runtime.Serialization.DataMember>attribute on all the properties that you want to access on the server.