简体   繁体   English

Javascript未显示来自GET springboot rest api的通知

[英]Javascript not showing notification from GET springboot rest api

I am totally new to javascript . 我对javascript完全陌生。 I just want to show a notification using notify.js when a button is clicked. 我只想在单击按钮时使用notify.js显示通知。

Following is my RestController code: 以下是我的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;
    }

Following is my js code: 以下是我的js代码:

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

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

});

I think I am missing some points but I am stuck. 我想我遗漏了一些要点,但我被困住了。 Any suggestions? 有什么建议么?

I tried using axios following is my js: 我尝试使用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")

              });
    });

Following response I get on brwoser console. 响应后,我进入brwoser控制台。 Now I just want that data field to be shown as notification : 现在,我只希望该数据字段显示为通知:

{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

You don't have anywhere http request. 您没有任何HTTP请求。 I recommend you using axios (because it's simple) Sample code: 我建议您使用axios (因为它很简单)示例代码:

axios.get('host:port/checkCurrentBranch')
.then(response => {
    // do what you want to do
})
.catch(error => {
    // do something if an error occurs
})

You need to properly use data key in axios response block. 您需要在axios响应块中正确使用数据密钥。

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

          });

In above code, I replaced data by response.data , because according your give code, data is not declared, that means its undefined. 在上面的代码中,我用response.data替换了data,因为根据您的给定代码,data没有声明,这意味着它是未定义的。 So if you mean data to that data which you get in axios response, then you need to access data by $.notify(response.data,"info") instead of $.notify(data,"info") 因此,如果将数据表示为在axios响应中获得的数据,则需要通过$.notify(response.data,"info")而不是$.notify(data,"info")访问数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM