0

我在我的 Java spring boot 应用程序中有一个简单的 ajax 调用,它调用控制器中的一个方法,然后将值返回到前端控制台。但是,当我运行代码时,它以状态运行,400但在我的控制台中没有显示任何内容。不确定我是否忘记了任何东西或者我的设置错误,但我假设没有任何东西被传回。

查询:

$(".modalPathContentBtn").on("click", function(event) {

                getSecondQuery();

            });

            function getSecondQuery() {

                var search2 = {
                    "dtoTiername" : "Test"

                }

                $.ajax({
                    type : "POST",
                    contentType : 'application/json; charset=utf-8',
                    dataType : 'json',
                    url : "/ajax/mqlGetSecondQuery",
                    data : JSON.stringify(search2),
                    success : function(result) {

                        console.log("It works: " + result);

                    }

                });
            }

爪哇:

@RequestMapping(value = "/ajax/mqlGetSecondQuery", method = RequestMethod.POST)
    public @ResponseBody String sendSecondQuery(@RequestBody TestApp mTestApp, HttpServletRequest request) {
        String pathVal = mTestApp.getDtoTiername();

        System.out.println("Test of if it is getting to this part of the code");

return "randomString";


    }
4

1 回答 1

1

您提到您的请求失败,状态代码为 400,这意味着success您的 ajax 请求不会被调用,因为请求没有成功。您需要添加一个失败处理程序。它看起来像这样。

$.ajax({
  type: "POST",
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
  url: "/ajax/mqlGetSecondQuery",
  data: JSON.stringify(search2),
  success: function(result) {
    console.log("It works: " + result);
  },
  fail: function(err) {
    console.log(err)
  }
});

这可能不是确切的语法,但这里的想法是您有一个失败处理程序

于 2020-01-09T23:22:52.180 回答