我正在尝试使用 Prototype/AJAX 访问 Web 服务,但遇到了一个我无法弄清楚的错误:似乎当我向服务器发出请求时,我的请求被解释为 OPTIONS 而不是 GET 请求(并反过来抛出一个 501 - 未实现的错误,因为服务器只允许 GET 请求,根据我的理解Access-Control-Request-Method:)。我是否在 AJAX/请求公式中遗漏了可能导致此错误的内容?我在这里阅读了一些 CORS/预检请求,但我不确定当我的代码看起来合规时它如何应用......
这是相关的 AJAX 请求:
function fetchMetar() {
var station_id = $("station_input").value;
new Ajax.Request(REQUEST_ADDRESS, {
method: "get",
parameters: {stationString: station_id},
onSuccess: displayMetar,
onFailure: function() {
$("errors").update("an error occurred");
}
});
}
这是我从 Chrome 获取的错误和相关请求信息:
Request URL:http://weather.aero/dataserver_current/httpparam?
dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3
&mostRecent=true&stationString=&stationString=KSBA
Request Method:OPTIONS
Status Code:501 Not Implemented
Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:origin, x-prototype-version, x-requested-with, accept
Access-Control-Request-Method:GET
Connection:keep-alive
Host:weather.aero
Origin:http://domain.com
Referer:http://domain.com/.../...html
我可以在这里俯瞰什么?为什么 Chrome 说请求是作为 OPTIONS 而不是 GET 发送的?当 Chrome 吐出Access-Control-Request-Headers:信息时,这些是请求中唯一允许的标头吗?
谢谢!