404从Jquery ajax调用WCF服务时未找到错误

404从Jquery ajax调用WCF服务时未找到错误,jquery,web-services,wcf,wcf-data-services,Jquery,Web Services,Wcf,Wcf Data Services,我正在尝试使用jquery ajax调用从html页面访问托管在IIS上的wcf服务,我无法访问该服务,其抛出404 not found错误,请问我是否应该更改jquery ajax调用或web配置文件,以访问托管在IIS或其他远程计算机上的服务 HTML页面: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="http://code.j

我正在尝试使用jquery ajax调用从html页面访问托管在IIS上的wcf服务,我无法访问该服务,其抛出404 not found错误,请问我是否应该更改jquery ajax调用web配置文件,以访问托管在IIS或其他远程计算机上的服务

HTML页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="json.js"></script>
    <title></title>
    <script type="text/javascript">
        var inputdata = { "userId": "101"};
        jQuery.support.cors = true;
        $.ajax({
            url: 'http://<ipaddress>/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;}
});
</script>
   </head>
<body>
</body>
</html>
接口:IService1.cs

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {

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

    }

}
网络配置文件:

 <?xml version="1.0"?>
    <configuration>

      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5"/>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="EndpBehavior">
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="ServiceBehavior" name="WcfService1.Service1">
            <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="EndpBehavior" bindingConfiguration="crossdomain"/>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost/WcfService1/Service1.svc"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <!--<protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>-->
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" >

        </serviceHostingEnvironment>
        <bindings>
          <webHttpBinding>
            <binding name="crossdomain" crossDomainScriptAccessEnabled="true"/>
          </webHttpBinding>
        </bindings>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>

        <directoryBrowse enabled="true"/>
        <httpProtocol>
    <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
    <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
    <add name="Access-Control-Max-Age" value="1728000" />
    </customHeaders>
    </httpProtocol>
      </system.webServer>

    </configuration>

更改您的运营合同

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

下面是另一种方法:

[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://<ipaddress>/WcfService1/Service1.svc/GetUserDetails/101'
type: 'GET',
//contentType: "application/json; charset=utf-8", No need to mention it for GET
url:'http:///WcfService1/Service1.svc/GetUserDetails/101'
键入:“GET”,
//contentType:“application/json;charset=utf-8”,无需在GET中提及
一切似乎都很好, 需要检查服务未提供404未在浏览器上找到。
如果您获得404,则需要在Windows server 8或12上的“WCF服务”下添加功能

现在它抛出500个内部服务器错误浏览并检查来自IISMemory gates的服务检查失败,因为可用内存(123486208字节)少于总内存的5%。因此,该服务将无法用于传入请求。要解决此问题,请减少计算机上的负载,或者调整serviceHostingEnvironment配置元素上的minFreeMemoryPercentageToActivateService的值。请按照建议,尝试在您的Web.config中将WCF主机的minFreeMemoryPercentageToActivateService设置为0
url: 'http://<ipaddress>/WcfService1/Service1.svc/GetUserDetails/101'
type: 'GET',
//contentType: "application/json; charset=utf-8", No need to mention it for GET