我試圖使用同春啓動web服務我有我的RestControllerPage:的JavaScript +春季啓動
@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了春天啓動服務器:
<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.
我真的鎖。
謝謝
我相信答案已經給出[這裏](http://stackoverflow.com/a/20035319/6148859)。 –
非常感謝:) – zaafrani