简体   繁体   English

在MVC 4应用程序中使用jQuery $ .ajax使用RESTful WCF服务

[英]Consume RESTful WCF service using jQuery $.ajax in MVC 4 application

For an MVC 4 application, I am trying to consume RESTful WCF service using jQuery $.ajax function and I am facing issues with it. 对于MVC 4应用程序,我正在尝试使用jQuery $ .ajax函数使用RESTful WCF服务,并且遇到了问题。

Below is my jQuery code, 以下是我的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) {
    }
});

}); });

Below is my WCF configuration, 以下是我的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>

Below is my WCF code, 以下是我的WCF代码,

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

The issue I am facing is, the WCF service implementation is not hit when I try debugging the code. 我面临的问题是,当我尝试调试代码时,没有找到WCF服务实现。 The service is not hit anytime. 该服务不会随时被点击。

Can anyone suggest if the code I am writing is the correct code or am I missing anything? 有人可以建议我编写的代码是正确的代码还是缺少任何内容?

Please suggest. 请提出建议。 Thanks in advance 提前致谢

try this change 'GET' to 'POST" 尝试将此“ 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) {
    }
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM