Javascript + Spring boot
Javascript + Spring boot
我正在尝试使用带有Spring Boot的Web服务我有我的RestControllerPage:
@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);
}
}
对于联系这个web服务,我在其他页面中使用Jquery从Spring启动服务器:
<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.
我真的被锁住了。
感谢
所以您正试图从JavaScript向不同的主机或端口进行API调用?由于同源政策,默认情况下不允许这样做。但是,您可以在Spring Boot中启用跨源请求在Spring Boot中启用CORS
必须将CORS头添加到请求的目标中。因此,如果Spring Boot应用程序在其上下文之外调用服务,则需要为该服务添加CORS支持。执行此操作的方式取决于运行服务的平台。
例如,在AWS上,您可以在设置中为S3网站设置CORS支持。
如果您可以控制其他服务,并且它是一个Spring Boot应用程序,那么您可以只为其他服务实现链接中的建议。
您可以使用@crossorgin注释来接受所有请求标头。
相关文章:
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- spring和angularJS(我得到了类似HTTP状态404的错误)
- Spring Ajax列表返回,但对象为空
- 使用 JQUERY AJAX (Spring MVC) 从控制器中删除
- 用Java/Struts/Spring项目实现Visual Captcha
- 在spring-boot中使用javascript在application.properties文件中获取值
- Javascript + Spring boot
- 使用带有 AngularJS(angular-websocket-service)和 Spring Boot 的 Web
- 使用 AngularJS 登录 Spring Boot $http.post
- 从Spring Boot控制器调用Javascript
- 使用Spring Boot在同一页面上重复提交表单
- 如何使用@requestMapping从JS到spring boot访问https协议
- Angular2的spring-boot项目结构
- 使用AngularJS和Spring Boot的Post基本认证语义
- 在node.js中上传文件,在spring boot中POST multipart
- 如何在Spring Boot中使用Backbone和REST进行身份验证
- Spring Boot Web静态资源
- 错误请求后请求在spring-boot和angularjs之间出现错误
- Spring Boot Google Maps SAXParseException