ajax调用映射的URL

时间:2017-02-26 17:46:30

标签: javascript java ajax

我正在尝试对我通过java托管的localhost 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";


    }

}

2 个答案:

答案 0 :(得分:1)

为什么不使用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>

答案 1 :(得分:0)

将{local}:8080 / highscore替换为http://localhost:8080/highscore

相关问题