【问题标题】:Javascript not showing notification from GET springboot rest apiJavascript未显示来自GET springboot rest api的通知
【发布时间】:2019-04-07 15:58:44
【问题描述】:

我对 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

【问题讨论】:

    标签: javascript java spring-boot


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      您没有任何 http 请求。 我推荐你使用axios(因为它很简单) 示例代码:

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

      【讨论】:

      • 是的,我已经尝试过了,但仍然无法使用 notify.js 显示通知。
      • 响应对象包含您刚刚得到的关键“数据”:response.data
      猜你喜欢
      • 2016-06-16
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      相关资源
      最近更新 更多