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),
    
    评论

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作