简体   繁体   English

Angular js如何传递数据以删除服务?

[英]Angular js how to pass data to delete service?

I am working in angular js on delete service api when passing data in service it is showing 400 bad request error.This is my js to call the service. 当在服务中传递数据时,我正在使用角度js在删除服务api上显示400错误的请求错误。这是我的js调用服务。

$scope.deleteUser = function(id){ 
    var data = 'Id='+id;   

     DataService.deleteUser(data).then(function successCallback(response) {
        console.log('deleted'); 
    }, function errorCallback(response) { 
            console.log(response);

    }); 

}

This is service js to delete users. 这是删除用户的服务js。

service.deleteUser = function(data){ 
                var config = {
                    headers:  {
                        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                        'X-Auth-Token': 'mytoken'
                    } 
                };
                return $http.delete(mainURL + '/users', data, config); 
};

This is curl api request where i need to pass data: 这是curl API请求,我需要在其中传递数据:

curl -v -H "X-Auth-Token: mytoken" -X DELETE -F Id=665799088 http://<ipaddress>/users

The 2nd parameter to $http.delete is the config, so you need to pass the config object as the 2nd parameter and not data. $ http.delete的第二个参数是配置,因此您需要将config对象作为第二个参数而不是数据。

DELETE method type doesn't accept a Request body so you should not be passing the ID as data. DELETE方法类型不接受Request正文,因此您不应将ID作为数据传递。 Instead try this http://main_url/users?Id=id 而是尝试使用此http:// main_url / users?Id = id

So in your service use this 所以在您的服务中使用

return $http.delete(mainURL + '/users?Id='+data, config);

Also check how you are passing the ID to your API. 还要检查如何将ID传递给API。 If you are passing ID as a query parameter then the above will work, but if you are passing it as a route parameter then the above URL won't work, but from your CURL expression I believe you are passing Id in the Query string and not as a route parameter. 如果您将ID作为查询参数传递,则上面的方法将起作用,但是如果您将其作为路由参数传递,则上述URL将不起作用,但是从您的CURL表达式中,我相信您将在查询字符串中传递ID,并且不作为路线参数。

According to documentation on $http service, delete method only accepts 2 parameters, url and config, meaning that your data is currently treated like config. 根据$http服务的文档 ,delete方法仅接受2个参数,即url和config,这意味着您的数据当前被视为config。 You are most likely looking to pass some params options to your config object. 您最有可能希望将一些params选项传递给配置对象。

DataService.deleteUser(id).then(function successCallback(response) {
    console.log('deleted'); 
}, function errorCallback(response) { 
        console.log(response);

}); 

And your service declaration should be more like. 和您的服务声明应该更像。

service.deleteUser = function(id){ 
                var config = {
                    headers:  {
                        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                        'X-Auth-Token': 'mytoken'
                    },
                    params : { 
                        id : id 
                    }
                };
                return $http.delete(mainURL + '/users', config); 
            };

Also keep in mind that a path looking like /users/id is more correct, if you are in charge of the API. 另外请记住,如果您负责API,则类似于/users/id的路径更正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM