Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

JavaScript Ajax GET request with Java Spring MVC controller

0 contributions
10 points

In JavaScript it is possible to make AJAX GET request in following way.

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

1. Pure JavaScript (Vanilla JS) AJAX GET request

In this section XMLHttpRequest object usage to make GET request is presented.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
<script>

    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == XMLHttpRequest.DONE) {
            if (xhr.status == 200) {
                document.body.innerText = 'Response: ' + xhr.responseText;
            } else {
                document.body.innerText = 'Error: ' + xhr.status;
            }
        }
    };

    xhr.open('GET', '/examples/echo?text=hello', true);
    xhr.send(null);

</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
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