2017-02-26 34 views
0

我正在尝试对通过java托管的本地URL进行AJAX调用。该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"; 


    } 

} 

回答

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> 
+0

谢谢!我试过这个,但是它返回了类中的所有内容,实际上是所有文本,而不仅仅是字符串。任何想法为什么? –