Spring Boot Jquery Controller Json doesn't append to the page
I'm having a simple rest controller which returns a string based on the params that it gets.My problem is that i'm trying to append the returned type to the page and my page basically gets updated with that returned type but it doesn't append the message to the page where the request was consumed.
The Controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public String sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
int totalUserMoney = loggedInUser.getTotalMoney();
if(money < 1) {
return "You can't send 0 money";
}
if(user == null) {
return "This user doesn't exist!";
}
if(user != null && money >=1) {
if(money > totalUserMoney) {
return "You can't send that much money";
}else {
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
return "Money sent successfully";
}
}
return null;
}
The form:
<h3 th:text="'Your current balance is: ' + ${money}"></h3>
<form th:action="@{/send}" METHOD = GET>
User:<br>
<input id="email" type="text" name="email">
<br>
Amount of Money To Send:<br>
<input id="money" type="text" name="money">
<br><br>
<input class="sendButton" type="submit" value="Submit">
</form>
<div id = "response"></div>
<script src="js/ajaxPost.js"></script>
Jquery:
$( document ).ready(function() {
$('.sendButton').click(function(){
ajaxPost();
});
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
dataType: "json",
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(response) {
$('#response').append('<h3>' + response + '</h3>');
console.log(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
})
After the form is submitted the page looks like this:
But i need to append this message to the page were the form was submitted:( Why is this happening?
jquery spring rest spring-boot controller
add a comment |
I'm having a simple rest controller which returns a string based on the params that it gets.My problem is that i'm trying to append the returned type to the page and my page basically gets updated with that returned type but it doesn't append the message to the page where the request was consumed.
The Controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public String sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
int totalUserMoney = loggedInUser.getTotalMoney();
if(money < 1) {
return "You can't send 0 money";
}
if(user == null) {
return "This user doesn't exist!";
}
if(user != null && money >=1) {
if(money > totalUserMoney) {
return "You can't send that much money";
}else {
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
return "Money sent successfully";
}
}
return null;
}
The form:
<h3 th:text="'Your current balance is: ' + ${money}"></h3>
<form th:action="@{/send}" METHOD = GET>
User:<br>
<input id="email" type="text" name="email">
<br>
Amount of Money To Send:<br>
<input id="money" type="text" name="money">
<br><br>
<input class="sendButton" type="submit" value="Submit">
</form>
<div id = "response"></div>
<script src="js/ajaxPost.js"></script>
Jquery:
$( document ).ready(function() {
$('.sendButton').click(function(){
ajaxPost();
});
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
dataType: "json",
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(response) {
$('#response').append('<h3>' + response + '</h3>');
console.log(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
})
After the form is submitted the page looks like this:
But i need to append this message to the page were the form was submitted:( Why is this happening?
jquery spring rest spring-boot controller
add a comment |
I'm having a simple rest controller which returns a string based on the params that it gets.My problem is that i'm trying to append the returned type to the page and my page basically gets updated with that returned type but it doesn't append the message to the page where the request was consumed.
The Controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public String sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
int totalUserMoney = loggedInUser.getTotalMoney();
if(money < 1) {
return "You can't send 0 money";
}
if(user == null) {
return "This user doesn't exist!";
}
if(user != null && money >=1) {
if(money > totalUserMoney) {
return "You can't send that much money";
}else {
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
return "Money sent successfully";
}
}
return null;
}
The form:
<h3 th:text="'Your current balance is: ' + ${money}"></h3>
<form th:action="@{/send}" METHOD = GET>
User:<br>
<input id="email" type="text" name="email">
<br>
Amount of Money To Send:<br>
<input id="money" type="text" name="money">
<br><br>
<input class="sendButton" type="submit" value="Submit">
</form>
<div id = "response"></div>
<script src="js/ajaxPost.js"></script>
Jquery:
$( document ).ready(function() {
$('.sendButton').click(function(){
ajaxPost();
});
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
dataType: "json",
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(response) {
$('#response').append('<h3>' + response + '</h3>');
console.log(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
})
After the form is submitted the page looks like this:
But i need to append this message to the page were the form was submitted:( Why is this happening?
jquery spring rest spring-boot controller
I'm having a simple rest controller which returns a string based on the params that it gets.My problem is that i'm trying to append the returned type to the page and my page basically gets updated with that returned type but it doesn't append the message to the page where the request was consumed.
The Controller:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public String sendMoney(@RequestParam String email, @RequestParam int money) {
User user = userRepository.findByEmail(email);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepository.findByEmail(auth.getName());
int totalUserMoney = loggedInUser.getTotalMoney();
if(money < 1) {
return "You can't send 0 money";
}
if(user == null) {
return "This user doesn't exist!";
}
if(user != null && money >=1) {
if(money > totalUserMoney) {
return "You can't send that much money";
}else {
user.setTotalMoney(user.getTotalMoney() + money);
userRepository.save(user);
return "Money sent successfully";
}
}
return null;
}
The form:
<h3 th:text="'Your current balance is: ' + ${money}"></h3>
<form th:action="@{/send}" METHOD = GET>
User:<br>
<input id="email" type="text" name="email">
<br>
Amount of Money To Send:<br>
<input id="money" type="text" name="money">
<br><br>
<input class="sendButton" type="submit" value="Submit">
</form>
<div id = "response"></div>
<script src="js/ajaxPost.js"></script>
Jquery:
$( document ).ready(function() {
$('.sendButton').click(function(){
ajaxPost();
});
function ajaxPost(){
$.ajax({
type: "GET",
url: "/send",
dataType: "json",
data: {
'email': $('#email').val(),
'money': $('#money').val(),
},
success: function(response) {
$('#response').append('<h3>' + response + '</h3>');
console.log(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
})
After the form is submitted the page looks like this:
But i need to append this message to the page were the form was submitted:( Why is this happening?
jquery spring rest spring-boot controller
jquery spring rest spring-boot controller
asked Dec 29 '18 at 13:35
Truica SorinTruica Sorin
737
737
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The javascript onclick handler you have on the submit button will get called, but you don't prevent the default behavior of the submit button which is to submit the form to the url specified by the action. Assuming you just want the javascript to handle it you could try changing the submit button to just a button
<input class="sendButton" type="button">Submit</input>
and/or change your click handler to prevent the default action:
$('.sendButton').click(function(event){
ajaxPost();
event.preventDefault();
});
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%2f53970047%2fspring-boot-jquery-controller-json-doesnt-append-to-the-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The javascript onclick handler you have on the submit button will get called, but you don't prevent the default behavior of the submit button which is to submit the form to the url specified by the action. Assuming you just want the javascript to handle it you could try changing the submit button to just a button
<input class="sendButton" type="button">Submit</input>
and/or change your click handler to prevent the default action:
$('.sendButton').click(function(event){
ajaxPost();
event.preventDefault();
});
add a comment |
The javascript onclick handler you have on the submit button will get called, but you don't prevent the default behavior of the submit button which is to submit the form to the url specified by the action. Assuming you just want the javascript to handle it you could try changing the submit button to just a button
<input class="sendButton" type="button">Submit</input>
and/or change your click handler to prevent the default action:
$('.sendButton').click(function(event){
ajaxPost();
event.preventDefault();
});
add a comment |
The javascript onclick handler you have on the submit button will get called, but you don't prevent the default behavior of the submit button which is to submit the form to the url specified by the action. Assuming you just want the javascript to handle it you could try changing the submit button to just a button
<input class="sendButton" type="button">Submit</input>
and/or change your click handler to prevent the default action:
$('.sendButton').click(function(event){
ajaxPost();
event.preventDefault();
});
The javascript onclick handler you have on the submit button will get called, but you don't prevent the default behavior of the submit button which is to submit the form to the url specified by the action. Assuming you just want the javascript to handle it you could try changing the submit button to just a button
<input class="sendButton" type="button">Submit</input>
and/or change your click handler to prevent the default action:
$('.sendButton').click(function(event){
ajaxPost();
event.preventDefault();
});
answered Dec 29 '18 at 13:57
pcoatespcoates
951128
951128
add a comment |
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%2f53970047%2fspring-boot-jquery-controller-json-doesnt-append-to-the-page%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