angularjs发送请求前端到后端

时间:2016-05-27 12:21:53

标签: angularjs rest

我正在使用jj的angularjs代码从前端发送删除请求

       var data = ersutils.getJsonCopy({"id" : $scope.resourceList[idx].id},$scope.resourceList[idx]);
        ResourceService.remove(data, function(data){
            //vm.dtInstance.reloadData(undefined, false);
            $modalInstance.close();
        }, function (resp){
            if(resp.status == 500){
                scope.modalinfo.message = "<code> this resource has booking(s) do you want to delete? </code>"; 
                ResourceService.remove({'id': delRes}, function(){
                    //vm.dtInstance.reloadData(undefined, false);
                    $modalInstance.close();
                })
            }else{
                scope.modalinfo.message = "<code> Unable to delete this Resource </code>";  
            }
        });

这里ersutils为多个参数提供jsoncopy ...当我将这些数据发送到rest api时,它说不支持的媒体类型  用于删除的restapi代码是

@DELETE
@Path("/{id:\\d+}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response removeRes(@PathParam("id") int id,Map<String, Object> source){  
    Map<String, Object> resp = new HashMap<>();
    //Map<String, Object> source=new HashMap<>();
    try{              
        resp.put("response",service.removeRes(id,source));
        return Response.status(Status.OK).entity(resp).build();
    }catch(FrontErsException e){
        resp.put("cause", e.getMessages());
        return Response.status(Status.CONFLICT).entity(resp).build();
    }catch(ErsException e){
        resp.put("cause", e.getMessages());
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(resp).build();
    }
}

错误是 angular.js:10514 DELETE http://localhost:8080/ers_dev/rest/resources/10?dynamicFields=%7B%22code%22 ... 2016-05-27%22%7D&amp; firstName = vinay&amp; isHuman = true&amp; name = N%2FA&amp; typeName = Employee 415(Unsupported Media Type)

1 个答案:

答案 0 :(得分:0)

您是否尝试使用@Consumes(MediaType.APPLICATION_FORM_URLENCODED)代替APPLICATION_JSON

不确切知道您为后端使用了什么,但是一般来说DELETE请求并不关心请求正文,因此params是URL编码的。