我在localhost:9393中运行的一个应用程序中有一个表单 发布数据的JS函数是:
function registerClient() {
var postData = $("#client-reg").serializeArray();
var formURL = "http://localhost:9393/mPaws/client/register?type=eSchool";
$.ajax({
url : formURL,
type : 'POST',
beforeSend: function (xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
},
data : postData,
success : function(data, textStatus, jqXHR) {
console.log(data);
registrationSuccessPage();
},
error : function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(jqXHR);
console.log(errorThrown);
}
});
}

我在弹簧控制器中有post方法:
@CrossOrigin(origins = "http://localhost:9696")
@RequestMapping(value = "/client/register", method = RequestMethod.POST)
@ResponseBody ValidationResponse registerClient(@ModelAttribute("user")User user, BindingResult errors, @Param String type) {
ValidationResponse response = new ValidationResponse();
getUserValidator().validate(user, errors);
if(errors.hasErrors()) {
response.setStatus("FAIL");
List<FieldError> allErrors = errors.getFieldErrors();
List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
for (FieldError objectError : allErrors) {
errorMesages.add(new ErrorMessage(objectError.getField(), objectError.getDefaultMessage()));
}
response.setErrorMessageList(errorMesages);
}else {
Role role = new Role();
if(type.equalsIgnoreCase("eSchool")) {
role.setId(3);
user.setType("eSchool");
}else if(type.equalsIgnoreCase("ePathLab")) {
role.setId(1);
user.setType("ePathLab");
}else {
role.setId(0);
}
User savedUser = null;
if(role.getId() > 0) {
user.setPassword(getUserService().getEncryptedPassword(user.getPassword()));
user.setEnabled(false);
user.setRole(role);
savedUser = getUserService().addUser(user);
}
if(savedUser != null) {
boolean mailSent = true;
if(mailSent){
response.setStatus("SUCCESS");
}
}else {
response.setStatus("FAIL");
}
}
return response;
}
&#13;
按下提交后我收到错误:
XMLHttpRequest无法加载http://localhost:9393/mPaws/client/register。对预检请求的响应没有通过访问控制检查:否&#39;访问控制 - 允许 - 来源&#39;标头出现在请求的资源上。起源&#39; http://localhost:9696&#39;因此不允许访问。响应的HTTP状态代码为403。
请帮助我。
答案 0 :(得分:1)
您必须提供跨域访问权限。您可以通过在回复中添加标题来完成此操作。
接入控制允许接头
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
response.addHeader("Access-Control-Max-Age", "1728000");
答案 1 :(得分:0)
ajax post功能:
function registerClient() {
var postData = $("#client-reg").serializeArray();
var formURL = "http://localhost:9393/mPaws/client/register?type=eSchool";
$.ajax({
url : formURL,
type : 'POST',
data : postData,
headers: {
"Access-Control-Allow-Origin" : "*"
},
success : function(data, textStatus, jqXHR) {
console.log(data);
registrationSuccessPage();
},
error : function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(jqXHR);
console.log(errorThrown);
}
});
}
&#13;
弹簧控制器中的post方法:
@CrossOrigin(origins = "http://localhost:9696")
@RequestMapping(value = "/client/register", method = RequestMethod.POST, headers = {"Access-Control-Allow-Origin=*"})
@ResponseBody ValidationResponse registerClient(@ModelAttribute("user")User user, BindingResult errors, @Param String type) {
ValidationResponse response = new ValidationResponse();
getUserValidator().validate(user, errors);
if(errors.hasErrors()) {
response.setStatus("FAIL");
List<FieldError> allErrors = errors.getFieldErrors();
List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
for (FieldError objectError : allErrors) {
errorMesages.add(new ErrorMessage(objectError.getField(), objectError.getDefaultMessage()));
}
response.setErrorMessageList(errorMesages);
}else {
Role role = new Role();
if(type.equalsIgnoreCase("eSchool")) {
role.setId(3);
user.setType("eSchool");
}else if(type.equalsIgnoreCase("ePathLab")) {
role.setId(1);
user.setType("ePathLab");
}else {
role.setId(0);
}
User savedUser = null;
if(role.getId() > 0) {
user.setPassword(getUserService().getEncryptedPassword(user.getPassword()));
user.setEnabled(false);
user.setRole(role);
savedUser = getUserService().addUser(user);
}
if(savedUser != null) {
boolean mailSent = true;
if(mailSent){
response.setStatus("SUCCESS");
}
}else {
response.setStatus("FAIL");
}
}
return response;
}
&#13;
工作正常。