0

I am making an api call to a Delete method like this -

$http.delete('http://localhost:45467/api/v1/proddetails/' + prodId, null )
    .success(function (data, status, headers, config) {
        if(data.status == "200")
        {
            console.log('data deleted');
        }
        deferred.resolve(data);
    })
    .error(function (data, status, headers, config) {
        console.log("some error occurred");
        deferred.reject(data);
    });

The delete endpoint looks like this -

[HttpDelete]
public HttpResponseMessage Delete(int productId)

When I run this and look into the Chrome console, I am presented with this error -

OPTIONS http://localhost:45467/api/v1/proddetails/34 
(index):1 XMLHttpRequest cannot load   
http://localhost:45467/api/v1/proddetails/34. No 'Access-Control-Allow-  
Origin' header is present on the requested resource. Origin 
'http://localhost:5100' is therefore not allowed access. The response had 
HTTP status code 405.

Can't seem to figure out what's wrong here. The other Get and Post requests are working fine. How to fix this?

Note1: I have CORS already enabled -

[EnableCors(origins: "*", headers: "*", methods: "*")]

Note2: I am making use of interceptors to add an auth token to each request. I am not sure if that is causing any issue.

Note3: This is how I define route in asp.net webapiconfig file -

config.Routes.MapHttpRoute(
            name: "ProdApi",
            routeTemplate: "api/v1/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

Thanks in advance.

sideshowbarker
  • 62,215
  • 21
  • 143
  • 153
Sam
  • 3,628
  • 10
  • 36
  • 66

3 Answers3

2

I finally figured out what the problem was. Actually this post was helpful - PUT and Delete not working with ASP.NET WebAPI and Database on Windows Azure

I just needed to replace productId param to id - which is present in webapiconfig config. I am not sure why this works. Ideally I should be able to put any name for a parameter and not bound by what I put in route. Hope someone can explain this.

Community
  • 1
  • 1
Sam
  • 3,628
  • 10
  • 36
  • 66
0

You're doing an HTTP request to a different domain than your page is on. The browser is blocking it for security reasons.

No 'Access-Control-Allow-Origin' header is present on the requested resource.

This question explains how to make DELETE http requests work with asp.net-web-api.

Community
  • 1
  • 1
Timothée Jeannin
  • 8,322
  • 2
  • 49
  • 62
0

Change $http.delete() to $http.del()

Abdullah Khan
  • 9,469
  • 3
  • 52
  • 64