我知道有很多问题已经就此错误提出了许多问题。但是,即使在我的服务器端设置了标头访问控制 - 允许来源不允许访问源URL
"Access-Control-Allow-Origin" : "*"
后,我仍然无法获得此工作。
这里是我的Spring MVC控制器的方法:
@RequestMapping(method=RequestMethod.GET, value="dummy/{num}")
@ResponseBody
public ResponseEntity<Result> dummy(@PathVariable String num)
{
int n = Integer.parseInt(num);
final Result result = new Result();
result.setAddition(n+20);
result.setMultiplication(n*20);
result.setSubtraction(n-20);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
ResponseEntity<Result> ent = new ResponseEntity<Result>(result,headers,HttpStatus.CREATED);
return ent;
}
这里是jQuery的
$.ajax({
url: "http://localhost:8010/Probe_Rest_Service/test/dummy/9",
type: "get",
crossDomain: true,
dataType: 'json',
headers: {
"Content-type" : "application/json"
},
success: function(data){
console.log("It worked!");
alert(data);
},
error: function(){
// enable the inputs
alert("error");
}
});
我的AJAX调用我打过电话从开发的HTTP客户端我的REST API的铬和它的工作原理罚款(响应头具有Access-Control-Allow-Origin:*设置)。但是,当我从我的HTML文件调用它时,我得到了错误。
我使用JBoss为我的REST API和tomcat主办我的客户端网页
请注意,'crossDomain:true'不是必需的。 – 2013-03-19 21:07:55
你是否正在使用chrome从网络服务器进行html文件测试? – 2013-03-19 21:10:42
@KevinB是的,这是正确的 – CuriousCoder 2013-03-19 21:15:48