我已经创建了一个支持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>
谢谢!
答案 0 :(得分:0)
我认为你不能这样称呼它。以下是有关如何使用wcf服务http://www.codeproject.com/Articles/42643/Creating-and-Consuming-Your-First-WCF-Service
的示例答案 1 :(得分:0)
WebInvoke属性为您提供了一些选项:
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string MyAwesomeServiceMethod(Decimal value)
{
return value.ToString("F2");
}