1

So I have a funny little problem.

When calling my API from an AngularJS client with the following code, everything works fine, except that my API only recieves an empty object as data:

var req = {
      method: 'Post',
      url: config.API + request.toLowerCase() + '/',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      data: data
}

 $http(req)
     .then(function(response){
          // handling response
     })

Now, if I remove the headers: { 'Content-Type': 'application/x-www-form-urlencoded' } or changes it to headers: { 'Content-Type': 'application/json' } I get the this error:

The requested resource does not support http method 'OPTIONS'

My API controller looks like this:

    [EnableCors("*", "*", "*")]
    [HttpPost]
    public string Post([FromBody]dynamic value)
    {
        // Do some stuff
        Response response = new Response();
        return JsonConvert.SerializeObject(response);
    }

What I'm looking for is a way to prevent the tiresome "OPTIONS not supported" error, and still be able to send dynamic data to my controller

EDIT
Regarding the duplicate, I've already tried their approach, hence why I already are using [EnableCors] in my controller. But if I add this:

public static void Register(HttpConfiguration config)
{
    config.EnableCors();
}

I get this error instead

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost:82' is therefore not allowed access

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michael Tot Korsgaard
  • 3,892
  • 11
  • 53
  • 89
  • 1
    It seems it's trying to make an OPTIONS request for some reason. Can you try changing `method: 'Post'` to `method: 'POST'` (all caps) - just a guess, but perhaps it's case-sensitive. – VLAZ Dec 25 '16 at 16:43
  • 3
    OPTIONS is a preflight request made internally by the browser as part of CORS spec. Has nothing to do with your client side code. You need to make sure server accepts that method – charlietfl Dec 25 '16 at 16:45
  • 1
    What is `data`? – guest271314 Dec 25 '16 at 16:46
  • Have you tried looking into [this question](http://stackoverflow.com/questions/24352519/the-requested-resource-does-not-support-http-method-options)? – Ilya Luzyanin Dec 25 '16 at 17:02
  • Possible duplicate of [How to support HTTP OPTIONS verb in ASP.NET MVC/WebAPI application](http://stackoverflow.com/questions/19095777/how-to-support-http-options-verb-in-asp-net-mvc-webapi-application) – georgeawg Dec 25 '16 at 17:05
  • @guest271314: `data` is a Javascript object which deferres from request to request – Michael Tot Korsgaard Dec 25 '16 at 17:22
  • 1
    If `data` is a `javascript` object why do you set `Content-Type` header to `application/x-www-form-urlencoded`? – guest271314 Dec 25 '16 at 17:23
  • @guest271314: I did that as a result of a suggestion to an older problem. I would like to change the `Content-Type` but doing so gives me the mentioned error ;-) – Michael Tot Korsgaard Dec 25 '16 at 17:26
  • 1
    Note, have not tried `angularjs` or `asp.net-web-api`. `string Post` appears at server, then `return JsonConvert.SerializeObject(response);`? Should not the settings be consistent? – guest271314 Dec 25 '16 at 17:32
  • @vlaz: Changing `Post` to `POST` didn't change anything :-( – Michael Tot Korsgaard Dec 25 '16 at 17:32
  • @guest271314: `JsonConvert.SerializeObject(response);` returns a `string` :-) – Michael Tot Korsgaard Dec 25 '16 at 17:35
  • To url encode a JavaScript object use the [AngularJS $httpParamSerializerJQLike Service](https://docs.angularjs.org/api/ng/service/$httpParamSerializerJQLike). – georgeawg Dec 25 '16 at 19:07

0 Answers0