1

我创建了一个支持 AJAX 的 WCF 服务,我想使用 POST 调用它。但是该服务是 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>

谢谢!

4

2 回答 2

0

我不认为你可以这样称呼它。这是一个关于如何使用 wcf 服务的示例http://www.codeproject.com/Articles/42643/Creating-and-Consuming-Your-First-WCF-Service

于 2013-05-22T14:32:20.843 回答
0

WebInvoke 属性为您提供了一些选项:

    [WebInvoke(Method = "POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    public string MyAwesomeServiceMethod(Decimal value)
    {
        return value.ToString("F2");
    }
于 2013-05-22T17:02:58.337 回答