我在Cross Origin Resource Sharing和Prototype中遇到了一些麻烦。我有一个简单的发布请求到外国资源,并且对于简单的发布请求,有一些必须满足的规则:跨原点资源共享原型JS
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
}
});
不知道如何拿到的样机送这样一个简单的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
,并通过原型请求创建的头:
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”
奇怪的是,在这两种情况下,Web服务器返回所请求的资源(我看到它的“资源” Chrome浏览开发者控制台的),但它接缝该原型不能访问它在某种程度上
这为我工作。谢谢 – FosAvance 2017-01-30 14:35:41