C# jquery$.ajax调用WCF返回404未找到错误
我是WCF服务的新手。我只是想通过$.ajax调用学习WCF,所以创建了一个测试应用程序,它有两个项目1。Wcf服务的WcfService1项目和使用该服务的另一个客户端asp.net应用程序当我尝试对服务进行ajax调用时,会显示错误404(未找到)当我运行WCF服务时,一切看起来都正常。即使我尝试从客户端C文件调用它,一切都正常。。客户端的代码是C# jquery$.ajax调用WCF返回404未找到错误,c#,jquery,asp.net,ajax,wcf,C#,Jquery,Asp.net,Ajax,Wcf,我是WCF服务的新手。我只是想通过$.ajax调用学习WCF,所以创建了一个测试应用程序,它有两个项目1。Wcf服务的WcfService1项目和使用该服务的另一个客户端asp.net应用程序当我尝试对服务进行ajax调用时,会显示错误404(未找到)当我运行WCF服务时,一切看起来都正常。即使我尝试从客户端C文件调用它,一切都正常。。客户端的代码是 <body> <form id="form1" runat="server"> <div> Enter a n
<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>
服务实现
namespace WcfService1
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
无论我错在哪里,请引导我。提前感谢。我将两个项目放在同一个解决方案中。那么,我如何运行Wcf服务项目呢?您正在发送POST请求,但您的Wcf具有WebGet属性。我尝试将WebGet属性更改为[WebInvoke(Method=“POST”,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)],但没有为我解决问题
<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);
}
}
}