我知道有很多問題已經就此錯誤提出了許多問題。但是,即使在我的服務器端設置了標頭訪問控制 - 允許來源不允許訪問源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