从Jquery ajax调用WCF服务时出现404找不到错误 [英] 404 Not Found error while calling WCF service from Jquery ajax

查看:102
本文介绍了从Jquery ajax调用WCF服务时出现404找不到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to access wcf service hosted on IIS from html page using jquery ajax call,I am not able to hit the service , Its throwing 404 not found error, Can i please know should i make changes in the jquery ajax call or web config file to access the service hosted in IIS or some other remote machine

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>

Wcf服务:

namespace WcfService1
{

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

        }
    }
}

接口:IService1.cs

Interface : IService1.cs

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {

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

    }

}

WebConfig文件:

WebConfig File:

 <?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]
[WebGet(UriTemplate = "/GetUserDetails/{strUserID}", 
ResponseFormat = WebMessageFormat.Json)] 
User GetUserDetails(string strUserID);

User是您的自定义类保存的详细信息.

And User is your custom class holding detail.

[DataContract]
public class User
{
   [DataMemeber]
   public in UserID {get; set;}

   // Remaining attributes here.
}

我对AJAX没有太多经验,但是我认为它应该可以工作.

I don't have much experience with AJAX but I think it should work.

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

这篇关于从Jquery ajax调用WCF服务时出现404找不到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆