I’m messing about with my first WCF service and I am having problems passing parameters.
My jQuery ajax call looks like this…
$.ajax({
type : 'POST',
async : true,
url : 'Service2.svc/GetStats',
contentType: 'application/json; charset=utf-8',
data : '{ i : 12 }',
dataType: 'json',
success : function(result) {
//success routine...
}
});
add my service code is like…
<OperationContract()> _
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Public Function GetStats(ByVal i As Integer) As Results
Dim returnValue As New Results
returnValue.iValue1 = 10
returnValue.iValue2 = 20
returnValue.sMessage = "You executed " + "sdfsd"
Return returnValue
End Function
Results class is a really simple holder class, two Integers and a String.
This service works fine if I remove the parameters from GetStats and remove the corresponding data property from my $.ajax object – I can access the three returned values on the client.
When the parameter i is included I get an Internal Server Error status:500.
I’m guessing I’m missing an attribute or something from my service Function?
Thanks for the responses.
I spent a while wrestling with this one and all I needed to do was…
So contrary to some of the other suggestions i do want to pass the JSON as a string. (I suppose one should really use a library such as Crockford’s JSON for this)
However I could only discover this after finding what the actual error was. I did this by throwing this snippet into my
web.configfile…I got this from mkdot.net and it should really be generated using SvcConfigEditor.exe – which I couldn’t find on my locked down machine. But just pasting that in seemed to work – and gave me a log file in my development folder – this actually told me the nature of the error.