我在跨源资源共享和原型方面遇到一些问题。我对外部资源有一个简单的发布请求,对于简单的发布请求,必须满足一些规则:
Content-Type 必须是 application/x-www-form-urlencoded、multipart/form-data 或 text/plain,简单请求不会使用 http 请求设置自定义标头,并且服务器必须设置 Access- Control-Allow-Origin 标头正确。
使用普通的 JavaScript XMLHttpRequest 一切正常,但使用 PrototypeJS 则无法工作,因为它接缝 Prototype 设置了一些自定义标头,而我不知道如何防止它。
我在原型中尝试过:
new Ajax.Request('some.foreign-host.com/res.php', {
method: 'post',
postBody: 'foo=bar',
contentType: 'application/x-www-form-urlencoded',
onSuccess: function(e){
// some custom code
}
});
知道如何让 Prototype 发送这样一个简单的 CORS 请求吗?
我有一个由普通文件创建的标头转储JavaScript XMLHttpRequest:
POST /bthesis/returnJSON.php HTTP/1.1
Host: foreign-host.com
Connection: keep-alive
Referer: this-host.com
Content-Length: 9
Origin: this-host.com
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
User-Agent: [...]
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
和由 a 创建的标头原型请求:
OPTIONS /bthesis/returnJSON.php HTTP/1.1
Host: foreign-host.com
Connection: keep-alive
Referer: this-host.com
Access-Control-Request-Method: POST
Origin: this-host.com
Access-Control-Request-Headers: X-Prototype-Version, X-Requested-With, Content-type, Accept
Accept: */*
User-Agent: [...]
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
原型使用完全不同的标头集...这会导致控制台中出现以下错误:
XMLHttpRequest 无法加载foreign-host.com/bthesis/returnJSON.php。 Access-Control-Allow-Headers 不允许请求标头字段 X-Prototype-Version。
拒绝获取不安全标头“X-JSON”
奇怪的是,网络服务器在这两种情况下都会返回所请求的资源(我在 Chrome 开发者控制台的“资源”视图中看到它),但它似乎原型无法以某种方式访问它