簡體   English   中英

發送數據AJAX到RequestBody

[英]Send data AJAX to RequestBody

這是一件簡單的事情,但是因此很難。 我知道它會很容易解決,但我找不到它很容易。 謝謝您的幫助。

這是我的代碼:

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);
        }
    })
});

它連接到

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

我想發送給請求正文而不是url參數。 這是什么問題?


EDIT1

很抱歉再次提出問題。 我解決了上述問題,並且出現了新問題。 我的Spring服務器回復了我400錯誤的請求。

這是我的java源代碼:

@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;
}

我的錯誤:

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]

指定使用body選項-默認情況下,GET使用查詢參數。

body: JSON.stringify(sample),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM