我正在尝试对我通过 java 托管的本地主机 URL 进行 AJAX 调用。该 URL 目前仅包含一个字符串。
但是当我尝试从 JavaScript 调用它时,我没有得到任何返回值。代码/URL 甚至不起作用。
Javascript代码(对于网站):
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类代码(当前正在运行):
@RestController
public class Server {
@RequestMapping("/highscore")
public String highScore() {
return "test";
}
}
最佳答案
为什么不使用 jQuery,如下所示
<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>
关于javascript - ajax 调用映射的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471974/