Ajax Post to Web Api (405 Method Not Allowed)

Having an issue when I am posting AngularJS data to Web API end point. From the client browser I receive:

405 (Method Not Allowed)
Response for preflight has invalid HTTP status code 405

I have two separate projects which both run in localhost. On my Web Api I have set EnableCors() on config.

If I set content-type of the header to:

 'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'

Then it's able to hit my Web API endpoint. However my object argument is null. Could this be XML format rather than JSON? How do I go about resolving this?

Client side code:

 function signUp(data) {
        $http({
            method: 'POST',
            url: 'http://localhost:15218/api/account/register',
            data: JSON.stringify(data),
            headers: {
                'Content-type': 'application/json'
            }

    }).then(function successCallback(response) {
            console.log(response);
        }, function errorCallback(response) {
            console.log(response);
        });
    }
}

Server Side Method signature:

    [HttpPost]
    [Route("Register")]
    public async Task<HttpResponseMessage> Register(UserCommand command)


ANSWERS:


You can enable cors on the service by adding global.asax file

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}

Response for preflight has invalid HTTP status code 405

Before making the POST request you want to make, the browser is making an OPTIONS request asking for permission.

The server is responding to the OPTIONS request without a 200 response. The error message tells you that explicitly. (Maybe Access-Control-Allow-Origin cause the problem)

So before anything else, you must check for type of request method. If it is OPTIONS , pass 200 response code.



 MORE:


 ? HTTP Error 405 method post is not supported by this URL error accessing solr/index.html
 ? Action Hook Inside Wordpress Plugin Shortcode
 ? http post in ionic and python server
 ? Send a request to an Office add-in
 ? HTTP post file with HttpURLConnection
 ? Best way to transfer more values from one server to another via an HTTP call?
 ? How do I HTTP-POST a JPG in Windows Phone Silverlight?
 ? Android application - Google Authorization via HTTP Post
 ? HttpPost Create MVC
 ? HTTPPost does not work asp mvc 3