【发布时间】:2014-03-18 19:39:18
【问题描述】:
我正在尝试将一个 JSON 对象发布到我的 Spring MVC 控制器,但我只收到一个 Access-Control-Allow-Origin 错误。
我的控制器:
@RequestMapping(value= "/add", method = RequestMethod.POST, headers = {"content-type=application/json"})
public @ResponseBody Reponse addUser(Model model, @RequestBody @Valid @ModelAttribute("user") User user, BindingResult result) {
if (result.hasErrors()) {
Reponse error = new Reponse();
// etc......
return error;
} else {
return service.addUser(user);
}
}
我的 Zepto 帖子:
this.addUser = function (valeur, callback) {
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:8080/AgenceVoyage/user/add',
data: JSON.stringify({"mail" : "toto@toto.fr" , "password" : "titi"}),
dataType: "json",
contentType: "application/json",
success: function(data) {
if(data.reponse == "OK") {
window.location = "main.html";
} else {
alert("PROBLEM");
}
},
error: function(xhr, type) {
alert("ERROR");
}
});
};
我尝试在 POST 请求中不使用字符串化,@RequestMapping 中没有标头。
我的结果:
选项http://127.0.0.1:8080/AgenceVoyage/user/add 否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:9000” 使用权。 zepto.min.js:2 XMLHttpRequest 无法加载
【问题讨论】:
标签: ajax spring spring-mvc post zepto