我的角度代码:
var user={id:null,username:'data',address:'',email:''};
$http.post("localhost:8080/Myservice/adduser",user).success(function(data) {
alert("data gaya");
alert(data);
}).error(function(data1, status, headers, config) {
alert( "nahi gaya"+headers+"config"+config);
});
这是我的Spring服务映射
@RequestMapping(value = "/adduser", method = RequestMethod.POST)
@ResponseBody
public String createUser1(HttpServletResponse response,@RequestBody User user) {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type");
System.out.println("Created a User " + user.getUsername());
String sss ="{\"employeeID\":\"101\"}";
return sss;
}
这是我的pojo课程
public class User {
private long id;
private String username;
private String address;
private String email;
public User(){
}
public User(long id, String username, String address, String email){
this.id = id;
this.username = username;
this.address = address;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
当我在没有发送任何数据的情况下进行邮件调用时它工作正常但是当我发布数据时它会发出此错误
XMLHttpRequest cannot load localhost:8080/Spring4MVCAngularJSExample/adduser. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
结果
当我使用mozilla的RESTclient调用我的服务时,它会给出错误
任何帮助都会非常明显。 提前谢谢。
答案 0 :(得分:0)
您必须在控制器方法上启用CORS。使用createUser1
注释@CrossOrigin(origins = "http://domain.com")
方法,我们只允许http://domain.com
发送跨源请求。
或者只是您可以在控制器方法或类上使用@CrossOrigin
进行注释,以便从所有域启用CORS。
一些有用的链接和文档 -
https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html