1

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?

2
  • 1
    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 response from the server is most probably negative, meaning that you need to configure CORS on the server. Commented Oct 24, 2015 at 12:39
  • Ah thanks so much. Yes, I had changed how CORS was configured when debugging this before and forgot to change it back. Feel free to post this as the answer and I will accept :) Commented Oct 24, 2015 at 12:52

1 Answer 1

2

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.

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

1 Comment

In short, Options asks for the permissions that the server gives to the user. If Delete is allowed, the DELETE method may be executed. Otherwhise, the call will be forbidden ( in angular)

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.