试图删除请求ajax 405错误 - java

时间:2018-06-07 11:43:45

标签: java jquery http-status-code-405 http-delete

我正在尝试使用来自客户端的ajax请求向我的java服务发出删除请求。但是当我在控制台上查看网络时,它会显示以下消息:

Request URL: http://localhost:8888/api.html/delete/AF
Request Method: DELETE
Status Code: 405 
Remote Address: [::1]:8888
Referrer Policy: no-referrer-when-downgrade

我的java代码如下所示:

@Path("/api.html")
public class WorldService {
     private CountryPostgresDaoImpl CountryPostgresDao = new CountryPostgresDaoImpl();

@Path("/delete/{code}")
@DELETE
@Produces("application/json")
public Response deleteCountry(@PathParam("code") String code) throws SQLException {
    Country country = getCountryByCode(code);
    if(!CountryPostgresDao.Delete(country)) {
        return Response.status(404).build();
    }
    return Response.ok().build();

}

客户端看起来像这样:

    $("body").on('click', '#delete', function(){
         var trid = $(this).closest('tr').attr('id');
         console.log(trid);
         $.ajax({
             url: '/api.html/delete/' + trid,
             type: 'DELETE',
             success: function(response) {
                 console.log(response);
             }
         });
     });

1 个答案:

答案 0 :(得分:0)

删除以下代码段中的初始斜杠

@Path("delete/{code}")
@DELETE