Spring Boot And Jquery accesing values from a hashmap on the front end
I have a rest controller in spring boot which returns a hash map where the key is an integer and the value is a string.My problem is that i need to show those values inside the page using jquery but i don't know how to access those values from that map:(
Here is the controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public Map sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
Map<Integer,String> returnedValues = new HashMap<>();
returnedValues.put(loggedInUser.getTotalMoney(), "Something went wrong");
String message = "";
if (money < 1) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send 0 money");
return returnedValues;
}
if (user == null) {
returnedValues.put(loggedInUser.getTotalMoney(), "This user doesn't exist");
return returnedValues;
}
if (user != null && money >= 1) {
if (!email.equals(auth.getName()) && money >= 1) {
if (money > loggedInUser.getTotalMoney()) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send that much money");
return returnedValues;
} else {
loggedInUser.setTotalMoney(loggedInUser.getTotalMoney()-money);
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
userRepository.save(loggedInUser);
returnedValues.put(loggedInUser.getTotalMoney(), "Money sent successfully");
return returnedValues;
}
}
}
return returnedValues;
}
JQuery:
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
contentType: 'application/json',
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(data) {
console.log("SUCCES");
console.log(data);
$('.currentBalance').text(data[0]);
$('#response').append('<h3>' + data[1] + '</h3>');
},
error : function(e) {
alert("Error!")
console.log("ERROR: ", e);
}
});
}
So console.log(data) -> shows the hash map in the console like this:
{2165: "Money sent successfully", 2167: "Something went wrong"}
How can i access the key and the value of that object? I've tried with data[0] and other similar combinations and it didn't really worked:(
My html page has a h3 tag:
<h3 class="currentBalance" th:text="'Your current balance is: ' + ${money}"></h3>
So, using jquery i have to modify the text from currentBalance with the text that comes from the hashMap.
I've tried doing this:
$('.currentBalance').text(data[0]);
But data[0] it's not working:(
java jquery ajax spring-boot
add a comment |
I have a rest controller in spring boot which returns a hash map where the key is an integer and the value is a string.My problem is that i need to show those values inside the page using jquery but i don't know how to access those values from that map:(
Here is the controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public Map sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
Map<Integer,String> returnedValues = new HashMap<>();
returnedValues.put(loggedInUser.getTotalMoney(), "Something went wrong");
String message = "";
if (money < 1) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send 0 money");
return returnedValues;
}
if (user == null) {
returnedValues.put(loggedInUser.getTotalMoney(), "This user doesn't exist");
return returnedValues;
}
if (user != null && money >= 1) {
if (!email.equals(auth.getName()) && money >= 1) {
if (money > loggedInUser.getTotalMoney()) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send that much money");
return returnedValues;
} else {
loggedInUser.setTotalMoney(loggedInUser.getTotalMoney()-money);
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
userRepository.save(loggedInUser);
returnedValues.put(loggedInUser.getTotalMoney(), "Money sent successfully");
return returnedValues;
}
}
}
return returnedValues;
}
JQuery:
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
contentType: 'application/json',
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(data) {
console.log("SUCCES");
console.log(data);
$('.currentBalance').text(data[0]);
$('#response').append('<h3>' + data[1] + '</h3>');
},
error : function(e) {
alert("Error!")
console.log("ERROR: ", e);
}
});
}
So console.log(data) -> shows the hash map in the console like this:
{2165: "Money sent successfully", 2167: "Something went wrong"}
How can i access the key and the value of that object? I've tried with data[0] and other similar combinations and it didn't really worked:(
My html page has a h3 tag:
<h3 class="currentBalance" th:text="'Your current balance is: ' + ${money}"></h3>
So, using jquery i have to modify the text from currentBalance with the text that comes from the hashMap.
I've tried doing this:
$('.currentBalance').text(data[0]);
But data[0] it's not working:(
java jquery ajax spring-boot
add a comment |
I have a rest controller in spring boot which returns a hash map where the key is an integer and the value is a string.My problem is that i need to show those values inside the page using jquery but i don't know how to access those values from that map:(
Here is the controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public Map sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
Map<Integer,String> returnedValues = new HashMap<>();
returnedValues.put(loggedInUser.getTotalMoney(), "Something went wrong");
String message = "";
if (money < 1) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send 0 money");
return returnedValues;
}
if (user == null) {
returnedValues.put(loggedInUser.getTotalMoney(), "This user doesn't exist");
return returnedValues;
}
if (user != null && money >= 1) {
if (!email.equals(auth.getName()) && money >= 1) {
if (money > loggedInUser.getTotalMoney()) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send that much money");
return returnedValues;
} else {
loggedInUser.setTotalMoney(loggedInUser.getTotalMoney()-money);
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
userRepository.save(loggedInUser);
returnedValues.put(loggedInUser.getTotalMoney(), "Money sent successfully");
return returnedValues;
}
}
}
return returnedValues;
}
JQuery:
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
contentType: 'application/json',
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(data) {
console.log("SUCCES");
console.log(data);
$('.currentBalance').text(data[0]);
$('#response').append('<h3>' + data[1] + '</h3>');
},
error : function(e) {
alert("Error!")
console.log("ERROR: ", e);
}
});
}
So console.log(data) -> shows the hash map in the console like this:
{2165: "Money sent successfully", 2167: "Something went wrong"}
How can i access the key and the value of that object? I've tried with data[0] and other similar combinations and it didn't really worked:(
My html page has a h3 tag:
<h3 class="currentBalance" th:text="'Your current balance is: ' + ${money}"></h3>
So, using jquery i have to modify the text from currentBalance with the text that comes from the hashMap.
I've tried doing this:
$('.currentBalance').text(data[0]);
But data[0] it's not working:(
java jquery ajax spring-boot
I have a rest controller in spring boot which returns a hash map where the key is an integer and the value is a string.My problem is that i need to show those values inside the page using jquery but i don't know how to access those values from that map:(
Here is the controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public Map sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
Map<Integer,String> returnedValues = new HashMap<>();
returnedValues.put(loggedInUser.getTotalMoney(), "Something went wrong");
String message = "";
if (money < 1) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send 0 money");
return returnedValues;
}
if (user == null) {
returnedValues.put(loggedInUser.getTotalMoney(), "This user doesn't exist");
return returnedValues;
}
if (user != null && money >= 1) {
if (!email.equals(auth.getName()) && money >= 1) {
if (money > loggedInUser.getTotalMoney()) {
returnedValues.put(loggedInUser.getTotalMoney(), "You can't send that much money");
return returnedValues;
} else {
loggedInUser.setTotalMoney(loggedInUser.getTotalMoney()-money);
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
userRepository.save(loggedInUser);
returnedValues.put(loggedInUser.getTotalMoney(), "Money sent successfully");
return returnedValues;
}
}
}
return returnedValues;
}
JQuery:
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
contentType: 'application/json',
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(data) {
console.log("SUCCES");
console.log(data);
$('.currentBalance').text(data[0]);
$('#response').append('<h3>' + data[1] + '</h3>');
},
error : function(e) {
alert("Error!")
console.log("ERROR: ", e);
}
});
}
So console.log(data) -> shows the hash map in the console like this:
{2165: "Money sent successfully", 2167: "Something went wrong"}
How can i access the key and the value of that object? I've tried with data[0] and other similar combinations and it didn't really worked:(
My html page has a h3 tag:
<h3 class="currentBalance" th:text="'Your current balance is: ' + ${money}"></h3>
So, using jquery i have to modify the text from currentBalance with the text that comes from the hashMap.
I've tried doing this:
$('.currentBalance').text(data[0]);
But data[0] it's not working:(
java jquery ajax spring-boot
java jquery ajax spring-boot
edited Dec 30 '18 at 17:16
Truica Sorin
asked Dec 30 '18 at 16:53
Truica SorinTruica Sorin
737
737
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Here is the sample code :
data = {2165: "Money sent successfully", 2167: "Something went wrong"};
var idx = 0;
var key = Object.keys(data)[idx];
var value = data[key]
console.log(value);
$('.currentBalance').text(value);
add a comment |
Here is the snippet:
for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(key + " -> " + data[key]);
}
}
I don't need to iterate through the map i just need to show the first value:( basically i need to modify this $('.currentBalance').text(data[0]); with the key value from the data
– Truica Sorin
Dec 30 '18 at 17:07
1
@TruicaSorin : Please update your question with html code.
– Govind Parashar
Dec 30 '18 at 17:14
I've edited the question
– Truica Sorin
Dec 30 '18 at 17:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979592%2fspring-boot-and-jquery-accesing-values-from-a-hashmap-on-the-front-end%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is the sample code :
data = {2165: "Money sent successfully", 2167: "Something went wrong"};
var idx = 0;
var key = Object.keys(data)[idx];
var value = data[key]
console.log(value);
$('.currentBalance').text(value);
add a comment |
Here is the sample code :
data = {2165: "Money sent successfully", 2167: "Something went wrong"};
var idx = 0;
var key = Object.keys(data)[idx];
var value = data[key]
console.log(value);
$('.currentBalance').text(value);
add a comment |
Here is the sample code :
data = {2165: "Money sent successfully", 2167: "Something went wrong"};
var idx = 0;
var key = Object.keys(data)[idx];
var value = data[key]
console.log(value);
$('.currentBalance').text(value);
Here is the sample code :
data = {2165: "Money sent successfully", 2167: "Something went wrong"};
var idx = 0;
var key = Object.keys(data)[idx];
var value = data[key]
console.log(value);
$('.currentBalance').text(value);
answered Dec 30 '18 at 17:32
Govind ParasharGovind Parashar
729720
729720
add a comment |
add a comment |
Here is the snippet:
for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(key + " -> " + data[key]);
}
}
I don't need to iterate through the map i just need to show the first value:( basically i need to modify this $('.currentBalance').text(data[0]); with the key value from the data
– Truica Sorin
Dec 30 '18 at 17:07
1
@TruicaSorin : Please update your question with html code.
– Govind Parashar
Dec 30 '18 at 17:14
I've edited the question
– Truica Sorin
Dec 30 '18 at 17:17
add a comment |
Here is the snippet:
for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(key + " -> " + data[key]);
}
}
I don't need to iterate through the map i just need to show the first value:( basically i need to modify this $('.currentBalance').text(data[0]); with the key value from the data
– Truica Sorin
Dec 30 '18 at 17:07
1
@TruicaSorin : Please update your question with html code.
– Govind Parashar
Dec 30 '18 at 17:14
I've edited the question
– Truica Sorin
Dec 30 '18 at 17:17
add a comment |
Here is the snippet:
for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(key + " -> " + data[key]);
}
}
Here is the snippet:
for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(key + " -> " + data[key]);
}
}
answered Dec 30 '18 at 17:06
Govind ParasharGovind Parashar
729720
729720
I don't need to iterate through the map i just need to show the first value:( basically i need to modify this $('.currentBalance').text(data[0]); with the key value from the data
– Truica Sorin
Dec 30 '18 at 17:07
1
@TruicaSorin : Please update your question with html code.
– Govind Parashar
Dec 30 '18 at 17:14
I've edited the question
– Truica Sorin
Dec 30 '18 at 17:17
add a comment |
I don't need to iterate through the map i just need to show the first value:( basically i need to modify this $('.currentBalance').text(data[0]); with the key value from the data
– Truica Sorin
Dec 30 '18 at 17:07
1
@TruicaSorin : Please update your question with html code.
– Govind Parashar
Dec 30 '18 at 17:14
I've edited the question
– Truica Sorin
Dec 30 '18 at 17:17
I don't need to iterate through the map i just need to show the first value:( basically i need to modify this $('.currentBalance').text(data[0]); with the key value from the data
– Truica Sorin
Dec 30 '18 at 17:07
I don't need to iterate through the map i just need to show the first value:( basically i need to modify this $('.currentBalance').text(data[0]); with the key value from the data
– Truica Sorin
Dec 30 '18 at 17:07
1
1
@TruicaSorin : Please update your question with html code.
– Govind Parashar
Dec 30 '18 at 17:14
@TruicaSorin : Please update your question with html code.
– Govind Parashar
Dec 30 '18 at 17:14
I've edited the question
– Truica Sorin
Dec 30 '18 at 17:17
I've edited the question
– Truica Sorin
Dec 30 '18 at 17:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979592%2fspring-boot-and-jquery-accesing-values-from-a-hashmap-on-the-front-end%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown