2011-06-24 42 views
0

当我试图让POST请求我WCF service,我不能够POST the service requestcan't get response.jQuery.POST:调用WCF服务不被调用!

我使用的WebHttpBindingWCF service is hosted in Windows servicePORT 8181

WCF服务方法:

[OperationContract] 
[WebInvoke(Method = "POST", UriTemplate = "/{cstid}/{deptid}/get/customer/?cstname={cstname}", 
    BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json)] 
Customer CustomerGet(string cstid, string deptid, string cstname); 

JQuery POST方法

jQuery.ajax({ 
    type: 'POST', 
    url: 'http://localhost:8181/mysite/e48/91/get/customer/?', 
    dataType: 'json', 
    contentType: "application/json; charset=utf-8", 
    processData: false, 
    success: function (data) { 
     alert(data); // not getting anything :(
    }, 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     alert('Error :' + textStatus); 
    } 
}); 

有没有人请让我知道我为什么不能调用这个服务,我该如何解决这个问题?

在此先感谢!

+0

你用firebug使用firefox吗?是在控制台中显示的帖子?答案是什么样的? – Patricia

+0

我没有答案,但是这是否使用REST套件用于WCF? – Mayo

+0

是否在构造的jquery调用中缺少服务url的查询字符串? –

回答

0

你UriTemplate:

/{cstid}/{deptid}/get/customer/?cstname={cstname} 

你的jQuery网址:

/e48/91/get/customer/? 

你好像缺少cstname,是因为查询字符串参数并不需要在运行时的值为null匹配一个UriTemplate。你不显示你的实现,但我猜测,但是你看起来Customer正在接受null,但是没有找到一个实际的实例,只是返回null。

+0

我认为这是CROSS DOMAIN发布的问题,因为我使用http:// localhost:8080 PORT运行我的MVC站点,而我的WCF服务由http:// localhost:8181 PORT承载......任何想法如何我解决这个问题? – nunu

0

由于您的UriTemplate是

UriTemplate = "/{cstid}/{deptid}/get/customer/?cstname={cstname}", 

你至少应该传递一个参数{} cstname。例如,试试这个:

jQuery.ajax({ 
type: 'POST', 
url: 'http://localhost:8181/mysite/e48/91/get/customer/?', 
data: { cstname: "nunu" }, 
dataType: 'json', 
contentType: "application/json; charset=utf-8", 
processData: false, 
success: function (data) { 
    alert(data); // not getting anything :(
}, 
error: function (XMLHttpRequest, textStatus, errorThrown) { 
    alert('Error :' + textStatus); 
}}); 

在我看来,使用WCF的Web HTTP服务比WebGet业务以外的任何其他是更多的麻烦比它的价值。

+0

我认为这是CROSS DOMAIN发布的问题,因为我使用http:// localhost:8080 PORT运行我的MVC站点,而我的WCF服务由http:// localhost:8181 PORT托管......任何想法如何我解决这个问题? – nunu

+0

是的。尝试(a)使用tyoe:“GET”而不是POST,或者(b)根本不指定contentType。对我而言,我一直得到(一)工作。而对于3个字符串参数,为什么你需要做POST?使用GET要容易得多,而且不太可能失败,这就是为什么我几乎总是使用类型“GET”。这是因为要让WCF服务正确使用POST是非常困难的。如果可能,请使用WebGet而不是WebInvoke –