0

I have made a simple WCF service, but I can't get in touch with the methods within the Service. It something to do with Cros Domain issues.

I have tried out selveral post and tutorials, but it simple wont work.

When I try to access CreateUser method, it returns a "Method not allowed"

In addition to above - The server is a remote server and I have access to the settings.

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>

Service

    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();
        }

    }
}

Interface

<%@ 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; }
    }

}

And 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>
K N
  • 279
  • 5
  • 22

0 Answers0