Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

javascript - What is a clean way to send a body with DELETE request?

I need to send a request body with my DELETE requests using $resource

The only way I could see to do this was to change:

https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js

From

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';

To

var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH' || action.method == 'DELETE';

Is there a better way to override this? Like when you alter the content type header you can do:

$httpProvider.defaults.headers["delete"] = {'Content-Type': 'application/json;charset=utf-8'};

Or something similar... Ive googled this but maybe Ive missed something obvious (not for the first time). Thanks for any help in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This works.

$scope.delete = function(object) {
    $http({
        url: 'domain/resource',
        method: 'DELETE',
        data: {
            id: object.id
        },
        headers: {
            "Content-Type": "application/json;charset=utf-8"
        }
    }).then(function(res) {
        console.log(res.data);
    }, function(error) {
        console.log(error);
    });
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...