我是WCF服务的新手。我只想学习带有$ .ajax调用的WCF,因此创建了一个测试应用程序,它有两个项目1.Wcf服务的WcfService1项目和另一个使用该服务的客户端asp.net应用程序。 当我尝试对服务错误404(未找到)进行ajax调用时显示 当我运行WCF服务时,一切正常。即使我尝试从客户端C#文件中调用它,一切都还可以。。客户的代码是
<body>
<form id="form1" runat="server">
<div>
Enter a number here :    <asp:TextBox runat="server" ID="txtbox"></asp:TextBox><br /><br />
<asp:Button runat="server" ID="btnClick" OnClick="btnClick_Click" Text="ClickMe"/>
</div>
</form>
<script type="text/javascript">
$('#txtbox').on("keyup", function () {
debugger;
var txt = parseInt($(this).val());
$.ajax({
url: "http://localhost:2393/Service1.svc/GetData",
type: "POST",
contentType: "application/json",
dataType:"jsonp",
data: JSON.stringify({ value: txt }),
success: function (data) {
alert(data);
console.log(data.d);
}
});
});
</script>
</body>
我对WcfService进行了简单的ajax调用,它只是回显输入的数字。 我的客户端Web.config文件是
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2393/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
我的WcfService web.config,接口(契约)后跟WcfService实现如下
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings />
<client />
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
服务合同
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "/GetData/{value}",
ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
}
}
服务实施
namespace WcfService1
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
如果我错了,请指导我。提前谢谢。