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.

6
  • looks like issue on server side stackoverflow.com/a/15619435/2435473 Commented Mar 23, 2015 at 11:41
  • I added it, but still no avail. Commented Mar 23, 2015 at 12:17
  • I am not sure what's going on. If anyone needs any info to dig into this, I can provide that. But this issues is going on weird level. Commented Mar 23, 2015 at 12:45
  • did you try to delete record from chrome extention postman on local? Commented Mar 23, 2015 at 12:49
  • I tried now, and it says - The requested resource does not support http method 'DELETE' Commented Mar 23, 2015 at 12:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

I have CORS enabled. Like I said, GET and POST requests are working fine.
In the error Chrome gives you, we can read No 'Access-Control-Allow-Origin' header you may have a problem with your CORS configuration. Are you sure you're including the correct headers ?
Have set methods:"*" in CORS.
You may need to specify explicitly the allowed methods GET, POST, PUT, DELETE, OPTIONS.
Check this question for more details : stackoverflow.com/questions/12521499/…
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.