首页 > 解决方案 > Javascript未显示来自GET springboot rest api的通知

问题描述

我对 javascript 完全陌生。我只想notify.js在单击按钮时显示通知。

以下是我的RestController代码:

@RequestMapping(value = "/checkCurrentBranch" , method=RequestMethod.GET)
    public String checkCurrectGitBranch(Model model, HttpServletResponse response) {

        String branchName = "";
        GitInfo gitInfo = new GitInfo();
        JsonFactory factory = new JsonFactory();
        String json = apiService.readGitProperties();
        ObjectMapper mapper = new ObjectMapper(factory);
        JsonNode rootNode;
        try {
            rootNode = mapper.readTree(json);

            Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
            while (fieldsIterator.hasNext()) {
                Map.Entry<String, JsonNode> field = fieldsIterator.next();
                if (field.getKey().toString().equalsIgnoreCase("git.branch")) {
                    branchName = field.getValue().toString();
                    adminAppLogger.info("Current Branch name :: "+branchName);
                }
            }
        } catch (IOException e) {
            adminAppLogger.error("Error while getting current Git branch :" + e);
            branchName = "Error while fetching branch name";
        }
        model.addAttribute("res", branchName);
        return branchName;
    }

以下是我的js代码:

$('#gitBranch').click(function(res) {

    /* <![CDATA[ */
    var path = /* [[@{/}]] */'checkCurrentBranch';
    /* ]]> */
        $.notify(res, "info");
        console.log(res);

});

我想我遗漏了一些观点,但我被卡住了。有什么建议么?

我尝试使用 axios 以下是我的 js:

$('#gitBranch').click(function(res) {

        /* <![CDATA[ */
        var path = /* [[@{/}]] */'checkCurrentBranch';
        /* ]]> */

        axios({
              method:'get',
              url:path,
              responseType:'json'
            })
              .then(function (response) {
                console.log(response)
                $.notify(data,"info")

              });
    });

响应后,我进入了浏览器控制台。现在我只想将该数据字段显示为通知:

{data: "qc_mediaworker_details", status: 200, statusText: "", headers: {…}, config: {…}, …}
config
:
{adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}
data
:
"qc_mediaworker_details"
headers
:
{pragma: "no-cache", date: "Sun, 04 Nov 2018 05:59:32 GMT", x-content-type-options: "nosniff", x-frame-options: "DENY", content-type: "application/json;charset=UTF-8", …}
request
:
XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status
:
200
statusText
:
""
__proto__
:
Object

标签: javascriptjavaspring-boot

解决方案


您需要在 axios 响应块中正确使用数据键。

axios({
          method:'get',
          url:path,
          responseType:'json'
        })
          .then(function (response) {
            console.log(response)
            $.notify(response.data,"info")

          });

在上面的代码中,我用 response.data 替换了 data ,因为根据你给出的代码, data 没有声明,这意味着它是未定义的。因此,如果您的意思是您在 axios 响应中获得的数据,那么您需要访问数据$.notify(response.data,"info")而不是$.notify(data,"info")


推荐阅读