javascript - 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
解决方案
您需要在 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")
推荐阅读
- python - 使用python检查zip文件中的任何文件夹
- javascript - 如何使用 react-native 更改 redux-form 的初始值?
- r - R中邻接矩阵中的最长路径
- php - 更改类中的构造函数参数后,如何标记任何需要更新的子类?
- r - 为 R 版本 3.5.0 安装 Choroplethr 时遇到问题
- apache-spark - 如何有效地批量重命名 pyspark 中的列?
- animation - 如何显示/隐藏/自动隐藏节点
- python - 如何获得具有最高 tf-idf 分数的前 n 个术语 - 大稀疏矩阵
- api - Adwords API 每秒查询次数限制
- python - 跨时间序列的列的相关性