Reputation: 11922
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?
Upvotes: 0
Views: 2565
Reputation: 11922
Thanks for the responses.
I spent a while wrestling with this one and all I needed to do was...
data : '{"i" : 12}' , //Object name i in double-quotes
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.config
file...
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="app_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
</system.diagnostics>
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.
Upvotes: 1
Reputation: 429
you are trying to send data as string. you have to send it as an object.
data : { i : 12 },
umm. it does not work on this.
try this.
data: 'i=12';
change your service like this and try again.
<OperationContract()> _
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Public Function GetStats(ByVal i As String) As Results
Dim returnValue As New Results
returnValue.iValue1 = 10
returnValue.iValue2 = 20
returnValue.sMessage = "You executed " + "sdfsd"
Return returnValue
End Function
Upvotes: 1
Reputation: 76880
you should do
var data = { i : 12 };
$.ajax({
type : 'POST',
async : true,
url : 'Service2.svc/GetStats',
contentType: 'application/json; charset=utf-8',
data : data,
dataType: 'json',
success : function(result) {
//success routine...
}
});
you are sending data as a string, while it should be an object
Upvotes: 0