I'm trying to post values to my WCF service via a jQuery AJAX call. Right now I have a single parameter that I'm trying to post. When I post the data, the correct service is called, but when I try to access the object str
I get the error Object reference not set to an instance of an object
.
How can I fix this?
I've already looked here and here.
My code:
$.ajax({
type: "POST",
url: '/api/addlocationfavorite',
data: JSON.stringify({locationid: 543}),
contentType: "application/json; charset=utf-8",
dataType: "json",
done: function (msg) {
alert('done!');
}
});
<Runtime.Serialization.DataContract>
Public Class SimpleLocation
<Runtime.Serialization.DataMember>
Public Property locationid() As Integer
Get
Return locationid
End Get
Set(ByVal value As Integer)
_locationid = value
End Set
End Property
Private _locationid As Integer
End Class
<OperationContract()>
<Web.WebInvoke(Method:="POST", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.WrappedRequest,
UriTemplate:="addlocationfavorite")>
Function addLocationFavorite(ByVal str As SimpleLocation) As Stream
Public Function addLocationFavorite(ByVal str As SimpleLocation) As Stream Implements Ilocation.addLocationFavorite
LogError("addLocationFavorite") '<-- this line is executed
LogError("addLocationFavorite str.locationid: " + str.locationid.ToString) '<--- this line throws error: 'Object reference not set to an instance of an object.'
End Function
I tried with and without quotes around the parameter name "locationid", but it shows the same error.