簡體   English   中英

如何解決 ajax 問題而不顯示在控制台中?

[英]How to fix ajax issue with not displaying in console?

我在我的 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";


    }

您提到您的請求失敗,狀態碼為 400,這意味着您的 ajax 請求不會success ,因為請求沒有成功。 您需要添加一個失敗處理程序。 它看起來像這樣。

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

這可能不是確切的語法,但這里的想法是您有一個失敗處理程序

暫無
暫無

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

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