2013-05-22 168 views
1

我創建了一個支持AJAX的WCF服務,我想用POST調用它。但服務是404沒有找到,我不明白爲什麼。我看到一些例子,但無法找到爲什麼我的服務無法訪問。我已經改變了我的網絡配置,但沒有什麼區別。我做錯了什麼?呼叫WCF服務404找不到

namespace ATSite 
{ 
    [ServiceContract(Namespace = "")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class SendEmailService 
    { 
     [OperationContract] 
     public string HelloWorld(string id) 
     { 
      return "Hello world " + id; 
     } 
    } 
} 

調用服務:

function helloWorld() { 
    $.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     url: "../SendEmailService.svc/HelloWorld", 
     data: '{"Id": "2"}', 
     dataType: "json", 
     success: function (result) { 
      onSuccess(result); 
     }, 
     error: alert('Erro') 
    }); 
} 
function onSuccess(result) { 
    alert(result); 
} 

這是我的web.config

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="ATSite.SendEmailServiceAspNetAjaxBehavior"> 
      <enableWebScript /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    <services> 
     <service name="ATSite.SendEmailService"> 
     <endpoint address="" behaviorConfiguration="ATSite.SendEmailServiceAspNetAjaxBehavior" 
      binding="webHttpBinding" contract="ATSite.SendEmailService" /> 
     </service> 
    </services> 
    </system.serviceModel> 

謝謝!

+0

你檢查../SendEmailService.svc/HelloWorld路徑是正確的? – Kuzgun

+0

我有完全相同的問題,你是否設法解決這個問題?將服務移動到另一個應用程序對我來說不是一種選擇,因爲功能很大程度上取決於它所處的項目。 –

回答

0

的WebInvoke屬性爲您提供了一些選項的示例:

[WebInvoke(Method = "POST", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json)] 
    public string MyAwesomeServiceMethod(Decimal value) 
    { 
     return value.ToString("F2"); 
    }