【问题标题】:Consume RESTful WCF service using jQuery $.ajax in MVC 4 application在 MVC 4 应用程序中使用 jQuery $.ajax 使用 RESTful WCF 服务
【发布时间】:2014-01-12 15:02:30
【问题描述】:

对于 MVC 4 应用程序,我正在尝试使用 jQuery $.ajax 函数使用 RESTful WCF 服务,但遇到了问题。

下面是我的 jQuery 代码,

$(document).ready(function () {
$.ajax({
    type: "GET",
    url: "http://localhost:55205/Services/UserService.svc/GetUserProjects",
    data: '{"gpn": "' + 1 + '"}',
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    success: function (data, textStatus, jqXHR) {
        alert(textStatus);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(textStatus);
    },
    complete: function (jqXHR, textStatus) {
    }
});

});

下面是我的 WCF 配置,

<system.serviceModel>
<services>
  <service name="BugTracker.WcfService.Services.UserService" behaviorConfiguration="BugTracker.WcfService.Services.UserServiceServiceBehavior">
    <endpoint address="" behaviorConfiguration="BugTracker.WcfService.Services.UserServiceAspNetAjaxBehavior"
      binding="webHttpBinding" contract="BugTracker.WcfService.Services.IUserService" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="BugTracker.WcfService.Services.UserServiceAspNetAjaxBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="BugTracker.WcfService.Services.UserServiceServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="" crossDomainScriptAccessEnabled="true"></binding>
  </webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"></standardEndpoint>
  </webHttpEndpoint>
</standardEndpoints>

下面是我的 WCF 代码,

 [ServiceContract]
public interface IUserService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetUserProjects/{gpn}")]
    IList<Project> GetUserProjects(String gpn);
}

我面临的问题是,当我尝试调试代码时,WCF 服务实现没有命中。该服务不会随时受到打击。

谁能建议我正在编写的代码是正确的代码还是我遗漏了什么?

请提出建议。提前致谢

【问题讨论】:

  • 所以你添加了一个断点,它没有被击中?您的客户是否仍然收到响应或错误?

标签: jquery ajax asp.net-mvc wcf


【解决方案1】:

尝试将“GET”更改为“POST”

$(document).ready(function () {
$.ajax({
    type: "POST",
    url: "http://localhost:55205/Services/UserService.svc/GetUserProjects",
    data: '{"gpn": "' + 1 + '"}',
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    success: function (data, textStatus, jqXHR) {
        alert(textStatus);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(textStatus);
    },
    complete: function (jqXHR, textStatus) {
    }
});
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-25
    • 2011-12-11
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2015-04-27
    相关资源
    最近更新 更多