从Jquery ajax调用WCF服务时出现404 Not Found错误

我试图使用jquery ajax调用从html页面访问IIS上托管的wcf服务,我无法点击服务,它抛出404找不到错误,我可以知道我应该在jquery ajax调用web配置中进行更改用于访问IIS或其他远程计算机中托管的服务的文件

HTML页面:

       var inputdata = { "userId": "101"}; jQuery.support.cors = true; $.ajax({ url: 'http:///WcfService1/Service1.svc/GetUserDetails', data: JSON.stringify(inputdata), type: 'POST', dataType : "jsonp", contentType: "application/json; charset=utf-8", //jsonpCallback: "handleResponse", success: function (result) { console.log(result.data); alert("success"); }, error: function (request, error) { alert('Network error has occurred please try again.Please check your connection and try again.'); return;} });      

Wcf服务:

 namespace WcfService1 { [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class Service1 : IService1 { public string GetUserDetails(string userId) { // Returns User Details } } } 

接口:IService1.cs

 namespace WcfService1 { [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)] int GetUserDetails(string strUserID); } } 

WebConfig文件:

                                <!--  -->                      

这是另一种方法:

 [OperationContract] [WebGet(UriTemplate = "/GetUserDetails/{strUserID}", ResponseFormat = WebMessageFormat.Json)] User GetUserDetails(string strUserID); 

User是您的自定义类,包含详细信息。

 [DataContract] public class User { [DataMemeber] public in UserID {get; set;} // Remaining attributes here. } 

我对AJAX没有太多经验,但我认为它应该有效。

 url: 'http:///WcfService1/Service1.svc/GetUserDetails/101' type: 'GET', //contentType: "application/json; charset=utf-8", No need to mention it for GET 

更改您的OperationContract,

  [OperationContract] [WebInvoke(UriTemplate = "/GetUserDetails", RequestFormat= WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")] int GetUserDetails(string strUserID); 

一切似乎都很好,需要检查服务是不是在浏览器上找不到404。 如果获得404,则需要在Windows服务器8或12上的“WCF服务”下安装“HTTP激活”function。