trees_are_great
trees_are_great

Reputation: 3911

Unable to sent DELETE to Web API from AngularJs with $http

I have an oData controller in my Web API 2 solution and I an trying to call the Delete action from my angularJs app. I have tried:

$http({
    method: 'DELETE',
    url: "http://localhost:52389/odata/Bears('" + $scope.itemKey + "')",
    headers: { 'Content-Type': 'application/json' }
}).then(function successCallback(response) {
    console.log('success', response);
}, function errorCallback(response) {
    console.log('error', response);
});

In fiddler this seems to send an OPTIONS request to the API. Inspecting the sent request in fiddler, I can see that this is what is sent:

OPTIONS http://localhost:52389/odata/Bears('1') HTTP/1.1
Host: localhost:52389
Connection: keep-alive
Access-Control-Request-Method: DELETE
Origin: http://localhost
X-FirePHP-Version: 0.0.6
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36
Access-Control-Request-Headers: accept
Accept: */*
Referer: http://localhost/?id=places
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

If I change, in fiddler, the first line from:

OPTIONS http://localhost:52389/odata/Bears('1') HTTP/1.1

to:

DELETE http://localhost:52389/odata/Bears('1') HTTP/1.1

Then this works. How do I get the method to be 'DELETE' from angularJs?

Upvotes: 1

Views: 231

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

An OPTIONS is sent because you're sending cross-origin requests. The server needs to agree with that first, hence the first OPTIONS request.

If no DELETE is sent after the OPTIONS, the response from the server is most probably negative, meaning that you need to configure CORS on the server.

Upvotes: 2

Related Questions