GET works, but POST fails when calling WCF from JQuery on remote server

814 views Asked by At

I have two OperationContracts on my test WCF service, when I test it locally both CheckGet and CheckPost work. When I call them both on the web server from the web server they both work, but when I call them from the remote server from my local machine, CheckGet works, but CheckPost hangs the browser window.

I have been banging my head against a brick wall all afternoon trying to work out why GET works, but POST doesn't. If anyone can suggest where the mistake is I'd be grateful.

    [OperationContract]
    [WebGet(UriTemplate = "oAuth/CheckGet")]
    string CheckGet();

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "oAuth/CheckPost", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string CheckPost();

Which just return a basic string

    public string CheckGet()
    {
        return "Get is working";
    }

    public string CheckPost()
    {
        return "Post is working";
    }

I am calling them like this:-

    function CheckGet() {
        $.ajax({
            cache: false,
            type: "GET",
            async: false,
            url: serviceRoot + "CheckGet",
            dataType: "json",
            timeout: (5 * 1000),
            success: function (message) {
                alert("Service says - " + message);
            },
            error: function (xhr) {
                alert(xhr.responseText);
            }
        });
    }

    function CheckPost() {
        $.ajax({
            cache: false,
            type: "POST",
            async: false,
            url: serviceRoot + "CheckPost",
            contentType: "application/json",
            dataType: "json",
            timeout: (5 * 1000),
            success: function (message) {
                alert("Service says - " + message);
            },
            error: function (xhr) {
                alert(xhr.responseText);
            }
        });
    }

This is in my Global.asax file

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(oAuth.Services.oAuth)));
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.Cache.SetNoStore();

            EnableCrossDmainAjaxCall();
    }

    private void EnableCrossDmainAjaxCall()
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }
}

UPDATE

It appears to be something to do with the Global.asax.cs file...

if I comment out HttpContext.Current.Response.End(); then I at least get a response, but if that's there then the Javascript hangs and I get a w3wp.exe error on the server.

1

There are 1 answers

1
Nick Zimmerman On

Your AJAX for your POST is requesting JSON as the contentType, which is not the default of WCF.

Try decorating your class like this:

    [OperationContract]
[WebInvoke(Method = "POST", 
 UriTemplate = "oAuth/CheckPost", 
 BodyStyle = WebMessageBodyStyle.WrappedRequest,
 ResponseFormat = WebMessageFormat.Json)]
string CheckPost();