将参数传递给WCF服务时发生内部服务器错误
我搞砸了我的第一个WCF服务,并且遇到了传递参数的问题。将参数传递给WCF服务时发生内部服务器错误
我的jQuery的AJAX调用看起来像这样...
$.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...
}
});
加我的服务代码是这样......
<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
类是一个非常简单的holder类,两个整数和一个串。
如果我从GetStats
中删除参数并从我的$ .ajax对象中删除相应的data
属性,我可以访问客户端上的三个返回值,此服务工作正常。
当包含参数i
时,我得到Internal Server Error
状态:500。
我猜我缺少一个属性或从我的服务功能?
感谢您的答复。
我花了,而这一个摔跤和所有我需要做的是...
data : '{"i" : 12}' , //Object name i in double-quotes
因此违背了其他一些建议,我做想通过JSON作为一个字符串。 (我想一个人应该真的使用这样的库,如Crockford's JSON)
但是我只能在找到实际的错误后才发现这个。我这样做是通过抛出这个片段到我web.config
文件...
<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>
我从mkdot.net得到这个,它应该真正利用SvcConfigEditor.exe生成 - 这我不能我锁定的机器上找到。但只是粘贴在似乎工作 - 并给我在我的开发文件夹中的日志文件 - 这实际上告诉我错误的性质。
您试图以字符串形式发送数据。你必须把它作为一个对象发送。
data : { i : 12 },
嗯。它不适用于此。
试试这个。
data: 'i=12';
更改您的服务,然后重试。
<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
你应该做的
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...
}
});
要发送的数据作为一个字符串,而它应该是一个对象
谢谢,我已经试过这个了。 – 2012-02-27 15:54:29
同样的错误恐怕 – 2012-02-27 15:54:41
@ElRonnoco我不熟悉vb,但是你的ajax调用没有任何问题。你有没有尝试过使用paremter静态地调用你的服务?应该有一个语法错误或st。 – littlealien 2012-02-27 15:59:01
@ElRonnoco我想,呃,你通过js发送数据。所以整数更改为字符串。你正试图把参数作为整数。也许你必须得到参数作为字符串,然后将其转换为整数的方法.. – littlealien 2012-02-27 16:01:16