lrony* 2019-06-29 07:41 采纳率: 0%
浏览 266

发送数据AJAX到RequestBody

It is a simple matter, but it is difficult because of this. I know it will be solved easily, but I can not find it easily. Thank you for your help.

It is my code :

var sample = {
    query: "kakao",
    x: "127.06283102249932",
    y: "37.514322572335935",
    radius: "20000"
};

$('#search').click(function (e) {
    $.ajax({
        url: "/map/search",
        contentType: 'application/json; charset=utf-8',
        dataType: "text",
        data: JSON.stringify(sample),
        success: function(data) {
            console.log(data);
        },
        error: function(error) {
            console.log(error);
        }
    })
});

and it is connect to

http://localhost:8080/map/search?{%22query%22:%22kakao%22,%22x%22:%22127.06283102249932%22,%22y%22:%2237.514322572335935%22,%22radius%22:%2220000%22}

I want to send to request body not url parameters. what is this problem?


EDIT1

I am sorry to ask further questions. I Solved above problem, and new problem occurs. My Spring server response to me 400 Bad Request.

It is my java source:

@RestController
@RequestMapping("/map")
public class MapSearchController {

    @Autowired
    private RestApiAccessor restApiAccessor;

    @RequestMapping(method = RequestMethod.GET, value = "/search")
    public MapSearchResponseDTO mapSearchRequest(@RequestBody MapSearchRequestDTO mapSearchRequestDTO) throws Exception {
        if (Objects.isNull(mapSearchRequestDTO.getQuery()))
            throw new IllegalAccessException("There is no Query parameter.");

        return restApiAccessor.mapSearchRequestGet(mapSearchRequestDTO);
    }
}

@Getter
@Setter
@ToString
public class MapSearchRequestDTO {
    private String query;
    private String category_group_code;
    private String x;
    private String y;
    private Integer radius;
    private String rect;
    private Integer page;
    private Integer size;
    private String distance;
}

My error:

2019-06-29 16:54:31.397  WARN 99354 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public map.search.dto.MapSearchResponseDTO map.search.controller.MapSearchController.mapSearchRequest(map.search.dto.MapSearchRequestDTO) throws java.lang.Exception]
  • 写回答

1条回答 默认 最新

  • weixin_33690367 2019-06-29 07:42
    关注

    Specify to use the body option - GET uses query parameters by default.

    body: JSON.stringify(sample),
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?