Javascript+;弹簧靴
我正在尝试使用带有Spring Boot的Web服务我有我的RestControllerPage:Javascript+;弹簧靴,javascript,jquery,spring-mvc,spring-boot,Javascript,Jquery,Spring Mvc,Spring Boot,我正在尝试使用带有Spring Boot的Web服务我有我的RestControllerPage: @RequestMapping( value="/api/user/connection", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_
@RequestMapping(
value="/api/user/connection",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<User> connect(@RequestBody Authentification authentification){
try {
if (authentification.getLogins() == "" || authentification.getLogins() == null) {
return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
}
String val = Md5.md5(authentification.getPwd());
int nbuser = userDao.countUser(authentification.getLogins(), val);
// we get the user for update the token
LinkedList<User> listUser = userDao.findByLoginsAndPwd(authentification.getLogins(), val);
if (listUser.get(0).getToken() == "" || listUser.get(0).getToken() == null) {
listUser.get(0).setToken(Md5.md5(listUser.get(0).getId() + listUser.get(0).getLogins()));
}
if (userDao.save(listUser.get(0)) == null) {
return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<User>(listUser.get(0),HttpStatus.OK);
}catch(Exception e){
return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
我真的锁死了
谢谢那么您正在尝试从JavaScript向其他主机或端口进行API调用?
由于同源策略,默认情况下不允许这样做。但是,您可以在Spring Boot中启用跨源请求,因此您正在尝试从JavaScript向其他主机或端口进行API调用?
由于同源策略,默认情况下不允许这样做。但是,您可以在Spring Boot中启用跨源请求,必须将CORS头添加到请求的目标。因此,如果SpringBoot应用程序调用其上下文之外的服务,则需要向该服务添加CORS支持。这样做的方式取决于运行服务的平台 例如,在AWS上,您可以在设置中为S3网站设置CORS支持
如果您可以控制其他服务,并且它是一个Spring引导应用程序,那么您可以只为其他服务实现链接中建议的内容 必须将CORS头添加到请求的目标。因此,如果SpringBoot应用程序调用其上下文之外的服务,则需要向该服务添加CORS支持。这样做的方式取决于运行服务的平台 例如,在AWS上,您可以在设置中为S3网站设置CORS支持
如果您可以控制其他服务,并且它是一个Spring引导应用程序,那么您可以只为其他服务实现链接中建议的内容 您可以使用@crossorigin annotation来接受所有请求头。您可以使用@crossorigin annotation来接受所有请求头。我相信已经给出了答案。我相信已经给出了答案。我正在测试此功能,谢谢,但我必须将我的html页面放在我的spring服务器上?不,本文的重点是全局激活CORS或针对特定控制器激活CORS,这样您就不需要在同一个Spring Boot应用程序中使用HTML+JavaScript。他们使用控制器,但本质上与您的RestController相同,因为它们也提供JSON。我正在测试这个,谢谢,但我必须将我的html页面放在我的spring服务器上?不,本文的重点是全局激活CORS或针对特定控制器激活CORS,这样您就不需要在同一个Spring Boot应用程序中使用HTML+JavaScript。它们使用控制器,但本质上与RestController相同,因为它们也提供JSON。
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#BtnValidate').click(function(){
$.ajax({
type: "POST",
contentType: 'application/json',
dataType:'json',
url:'http://localhost:9000/api/user/connection',
data:JSON.stringify({
logins:$('#Txtlogin').val(),
pwd:$('#TxtPwd').val()
}),
success: function(data){
$('.notification').html("<p> response : "+data.d+"</p>");
},
error:function(){
alert('Error in the webService');
}
});
return false;
});
});
</script>
</head>
<body>
<label>Login : </label>
<input type="text" id="Txtlogin">
<br/>
<label>Pwd : </label>
<input type="pwd" id="TxtPwd">
<br/>
<input type="button" id="BtnValidate" value="validate"/>
<div class="notification">
</div>
</body>
</html>
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.