2014-01-12 39 views
0

對於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服務實現是不是當我嘗試調試打碼。該服務不會隨時發生。

任何人都可以建議,如果我寫的代碼是正確的代碼或我缺少什麼?

請建議。在此先感謝

+0

所以,你已經加入了斷點「POST」,它不是beeing打? 是您的客戶端仍然得到響應或錯誤? – khlr

回答

0

嘗試這種變化「GET」來

$(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) { 
    } 
}); 
}); 
相關問題