Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

jQuery - Ajax POST request with Java Spring MVC controller

0 contributions
11 points

In this article, we're going to have a look at how to make make AJAX POST request with jQuery.

Note: scroll to See also section to see other variants of AJAX requests.

1. jQuery AJAX POST request

In this section $.ajax method is used to make POST request.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>  
</head>
<body>
<script>

    $(document).ready(function() {
      	$.ajax({
            type: 'POST',
            url: '/examples/echo-message',
            data: {
                message: 'hello'
            },
            success: function(text) {
                $(document.body).text('Response: ' + text);
            },
            error: function (jqXHR) {
                $(document.body).text('Error: ' + jqXHR.status);
            }
        });
    });

</script>
</body>
</html>

2. Spring MVC server site POST methods example

In this section simple Spring backend that handle POST method requests is presented.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EchoPostController {

    @RequestMapping(value = "/examples/echo-message", method = RequestMethod.POST)
    @ResponseBody
    public String sendPostMessage(@RequestParam("message") String message) {
        return message;
    }
}

See also

  1. JavaScript Ajax POST request with Java Spring MVC controller
  2. JavaScript - how to make ajax request?

References

  1. Ajax (programming) - Wikipedia
  2. XMLHttpRequest.prototype.send method - MDN Docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more