【问题标题】:jQuery.POST : Call WCF Service not being called !jQuery.POST:调用 WCF 服务未被调用!
【发布时间】:2011-09-22 01:11:56
【问题描述】:

当我尝试向我的 WCF service 发出 POST 请求时,我无法向 POST the service requestcan't get response. 发出 POST 请求

我正在使用 WebHttpBinding 和我的 WCF 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);
    }
});

谁能告诉我为什么我无法调用此服务以及如何解决此问题?

提前致谢!

【问题讨论】:

  • 你在使用 firefox 和 firebug 吗?帖子是否显示在控制台中?响应是什么样的?
  • 我没有答案,但这是否使用 WCF 的 REST 工具包?
  • 在构造的 jquery 调用中缺少服务 url 的查询字符串是错字吗?
  • 我认为跨域发布存在问题,因为我正在使用 localhost:8080 PORT 运行我的 MVC 站点,而我的 WCF 服务是使用 localhost:8181 PORT 托管的......知道我会怎么做解决这个问题?

标签: wcf jquery wcf-client jquery-post


【解决方案1】:

因为你的 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 操作以外的任何事情都是麻烦大于价值的。

【讨论】:

  • 我认为跨域发布存在问题,因为我正在使用 localhost:8080 PORT 运行我的 MVC 站点,而我的 WCF 服务是使用 localhost:8181 PORT 托管的......知道我会怎么做解决这个问题?
  • 是的。尝试 (a) 使用 tyoe: "GET" 而不是 POST,或者 (b) 根本不指定 contentType。对我来说,我总是得到(a)工作。而对于3个字符串参数,为什么需要POST呢?使用 GET 要容易得多,而且失败的可能性要小得多,这就是为什么我几乎总是使用 type:"GET"。这是因为很难让 WCF 服务与 POST 一起正常工作。尽可能使用 WebGet 而不是 WebInvoke
【解决方案2】:

你的 UriTemplate:

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

您的 jQuery 网址:

/e48/91/get/customer/?

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

【讨论】:

  • 我认为跨域发布存在问题,因为我正在使用 localhost:8080 PORT 运行我的 MVC 站点,而我的 WCF 服务是使用 localhost:8181 PORT 托管的......知道我会怎么做解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
相关资源
最近更新 更多