Languages
[Edit]
EN

jQuery - Ajax GET request with Java Spring MVC controller

1 points
Created by:
Root-ssh
175400

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

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

1. jQuery AJAX GET request

In this section $.ajax method is used to make GET 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: 'GET',
            url: '/examples/echo',
            data: {
                text: '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 GET methods example

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class EchoGetController {

    @RequestMapping(value = "/examples/echo", method = RequestMethod.GET)
    @ResponseBody
    public String makeGetEcho(@RequestParam("text") String text) {
        return text;
    }
}

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

jQuery

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join