我正在尝试从我的本地开发环境向服务器发送Ajax请求,使用的是:
<!DOCTYPE html>
<html>
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function() {
var params = {
format: "JSON",
season: "2015REG"
// Request parameters
};
$.ajax({
url: "https://<mysite>/sub/" + params.format + "/" + params.season,
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","<mykey>");
},
type: "GET"
})
.done(function(data) {
alert("success");
console.log("Data: \n" + JSON.stringify(data));
})
.fail(function(error) {
alert("error");
console.log("Error" + JSON.stringify(error));
});
});
</script>
</body>
</html>
当我查看Firefox时,发现它实际上发送了一个OPTIONS请求 - 但是服务器没有做出响应(404未找到),因此我得到了一个跨域请求阻止错误。
OPTIONS
XHR
https://mysite [HTTP/1.1 404 Resource Not Found 2437ms]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://mysite (Reason: CORS header 'Access-Control-Allow-Origin' missing).
我做错了什么?为什么jquery试图在我明确发送GET时询问选项?此外,我不拥有这个服务器,它属于另一家公司,该公司具有订阅API,因此我无法提供密钥或任何其他帮助来进行故障排除。