更新数据库的Ajax PUT REST请求

Ajax PUT REST request to update database

本文关键字:REST 请求 PUT Ajax 数据库 更新      更新时间:2023-09-26

我在javascript内调用我的REST api时遇到麻烦,根据我的测试,我收到错误的请求或不支持的媒体类型…

我的Servlet。

   /**
    * Update an existing frame
    * 
    * @param id the id of the <frameConfig> to update
    * @param frameConfig the updated data
    * @return the Updated <frameConfig>
    */
   @Path("/frame/{id}/")
   @PUT
   @Consumes(MediaType.APPLICATION_JSON)
   public Response updateFrame(@PathParam("id") String id, final FrameConfig frameConfig) {
      String idOfTheUpdatedFrame = FrameService.getInstance().updateFrame(id, frameConfig);
      URI uri = uriInfo.getAbsolutePathBuilder().path(idOfTheUpdatedFrame).build();
      return Response.ok(uri).build();
   }

我的javascript:

            $.ajax({ 
                headers: { 
                    'Accept': 'application/json',
                    'Content-Type': 'application/json' 
                },
                   type: "PUT",
                   dataType: "json",
                   url: "http://localhost:8080/densite-simulator-WebApp/rest/frames/frame/" + configToUpdate[0].id,
                   data: configToUpdate[0],
                   success: function(data){        
                     alert(data);
                   }
                });

Ajax调用中的数据类型是用于调用成功后检索到的数据,还是用于发送的数据类型?

datatype属性用于response预期返回的数据格式。如果没有指定,Javascript将尽力猜测应该是什么类型…