javascript 如何将对象参数传递给 WCF 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17913145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass an object parameter to a WCF service?
提问by amarruffo
I'm having this error:
我有这个错误:
Operation 'Login' in contract 'Medicall' has a query variable named 'objLogin' of type 'Medicall_WCF.Medicall+clsLogin', but type 'Medicall_WCF.Medicall+clsLogin' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.
I'm trying to pass a parameter to my WCF service, but the service isn't even showing.
我正在尝试将参数传递给我的 WCF 服务,但该服务甚至没有显示。
#region Methods
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Int32 Login(clsLogin objLogin)
{
try
{
// TODO: Database query.
if (objLogin.username == "" & objLogin.password == "")
return 1;
else
return 0;
}
catch (Exception e)
{
// TODO: Handle exception error codes.
return -1;
}
}
#endregion
#region Classes
[DataContract(), KnownType(typeof(clsLogin))]
public class clsLogin
{
public string username;
public string password;
}
#endregion
I'm using this:
我正在使用这个:
$.ajax({
url: "PATH_TO_SERVICE",
dataType: "jsonp",
type: 'post',
data: { 'objLogin': null },
crossDomain: true,
success: function (data) {
// TODO: Say hi to the user.
// TODO: Make the menu visible.
// TODO: Go to the home page.
alert(JSON.stringify(data));
},
failure: function (data) { app.showNotification('Lo sentimos, ha ocurrido un error.'); }
});
To call the service, it worked before with a service that recieved 1 string parameter. How can I recieve this object?
为了调用该服务,它之前使用了一个收到 1 个字符串参数的服务。我怎样才能收到这个对象?
采纳答案by p e p
The problem is that your Login
function is marked with the attribute WebGet[WebGet(ResponseFormat = WebMessageFormat.Json)]
. You should instead declare your method as WebInvoke:
问题是您的Login
函数标有属性WebGet[WebGet(ResponseFormat = WebMessageFormat.Json)]
。您应该改为将您的方法声明为WebInvoke:
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
public Int32 Login(clsLogin objLogin)
WebGet by default uses a QueryStringConverter class which is unable to convert your complex type. There is a way to get this to work for you if you really need to use WebGet, check out the discussion herefor a good explanation of how you would accomplish that.
默认情况下,WebGet 使用 QueryStringConverter 类,该类无法转换您的复杂类型。如果您确实需要使用 WebGet,有一种方法可以让您使用它,请查看此处的讨论,以获得有关如何实现这一目标的良好解释。
Take a look at this article for an explanation of WebGet vs WebInvoke. The basics is WebGet should be used with HTTP GET and WebInvoke should be used with other verbs like POST.
查看这篇文章,了解WebGet 与 WebInvoke的解释。基本原理是 WebGet 应该与 HTTP GET 一起使用,而 WebInvoke 应该与其他动词(如 POST)一起使用。