Reputation: 1
I am trying to make an AJAX call to a localhost URL that I am hosting via java. The URL currently only holds a String.
But when I try to call it from my javascript, I don't get any return value. The code/URL doesn't even work.
Javascript code(For website):
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(true){
alert('hello!');
document.getElementById('newgame').innerHTML = xhr.responseText;
}
};
xhr.open('GET', 'localhost:8080/highscore', true);
xhr.send(null);`
JAVA class code(currently running):
@RestController
public class Server {
@RequestMapping("/highscore")
public String highScore() {
return "test";
}
}
Upvotes: 0
Views: 297
Reputation: 89
Why you don't using jQuery instead, as below
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
req_url = "http://localhost:8080/highscore";
$.ajax({
type:"post",
url:req_url,
success:function(data){
alert('hello!');
document.getElementById('newgame').innerHTML = data;
}
});
});
</script>
</head>
<body>
</body>
</html>
Upvotes: 1
Reputation: 89
Replace localhost:8080/highscore by http://localhost:8080/highscore
Upvotes: 0