WCF - 我无法在我的新Web服务中访问我的方法

时间:2014-01-22 14:56:06

标签: jquery wcf

我已经制作了一个简单的WCF服务,但我无法与服务中的方法取得联系。它与Cros Domain问题有关。

我已经尝试了几篇文章和教程,但它很简单。

当我尝试访问CreateUser方法时,它返回“Method not allowed”

除上述之外 - 服务器是远程服务器,我可以访问这些设置。

Ajax Post

    <script type="text/javascript">
    function createUser() {
        var user = {};
        user.Username = $("[id*=userName_input]").val();
        user.Password = $("[id*=userPass_input]").val();
        user.Country = $("[id*=userCountry_input]").val();
        user.Email = $("[id*=userEmail_input]").val();
        $.ajax({
            type: "POST",
            url: "[url]/AttService.svc/CreateUser",
            data: JSON.stringify(user),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData: false,
            success: function (response) {
                alert("User has been added successfully.");
                window.location.reload();
            }
        });
        return false;
    }
</script>

服务

    namespace AttenttoWcfService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AttenttoService : IAttenttoService
    {
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public void CreateUser(User user)
        {
            SqlConnection con = new SqlConnection("Data Source=;Initial Catalog=Registration;User ID=xxx;Password=xxx");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into RegistrationTable(UserName,Password,Country,Email) values(@UserName,@Password,@Country,@Email)", con);
            cmd.Parameters.AddWithValue("@UserName", user.Username);
            cmd.Parameters.AddWithValue("@Password", user.Password);
            cmd.Parameters.AddWithValue("@Country", user.Country);
            cmd.Parameters.AddWithValue("@Email", user.Email);
            int result = cmd.ExecuteNonQuery();
        }

    }
}

接口

<%@ ServiceHost Language="C#" Debug="true" Service="AttenttoWcfService.AttenttoService" CodeBehind="AttService.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"%>




namespace AttenttoWcfService
{
    [ServiceContract(Namespace = "JsonpAjaxService")]
    public interface IAttenttoService
    {
        [OperationContract]
        void CreateUser(User user);

    }

    [DataContract]
    public class User
    {
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string Country { get; set; }
        [DataMember]
        public string Email { get; set; }
    }

}

和Web.Config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appSettings />
    <connectionStrings />
    <system.web>
        <compilation debug="true">
            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
            </assemblies>
        </compilation>
        <authentication mode="None" />
        <!--<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>-->

    </system.web>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"></modules>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="Content-Type" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5" />
                <providerOption name="WarnAsError" value="false" />
            </compiler>
        </compilers>
    </system.codedom>
    <system.serviceModel>



        <services>
            <service behaviorConfiguration="ServiceBehavior" name="AttenttoWcfService.AttenttoService">
                <endpoint address="basic" binding="webHttpBinding" contract="AttenttoWcfService.IAttenttoService" behaviorConfiguration="EndpBehavior" />
            </service>
        </services>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>

        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="EndpBehavior">
                    <webHttp />
                </behavior>
            </endpointBehaviors>
        </behaviors>

    </system.serviceModel>
</configuration>

0 个答案:

没有答案