Javascript Prototype.js 1.4.0抛出和#x27;拒绝设置不安全的标题“;“连接”';错误
我试图通过添加Javascript Prototype.js 1.4.0抛出和#x27;拒绝设置不安全的标题“;“连接”';错误,javascript,Javascript,我试图通过添加setRequestHeaders函数if()和continue来覆盖prototype.js避免指定的标题: for (var i = 0; i < requestHeaders.length; i += 2){ if("Connection" === requestHeaders[i] || "Connection" === requestHeaders[i+1]) continue; this.transport.setReq
setRequestHeaders
函数if()
和continue来覆盖prototype.js
代码>避免指定的标题:
for (var i = 0; i < requestHeaders.length; i += 2){
if("Connection" === requestHeaders[i] || "Connection" === requestHeaders[i+1])
continue;
this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
}
谢谢!:) 因此,您似乎希望在不接触库代码本身的情况下更改Prototype的行为
这不是问题,您可以在运行时交换掉setRequestHeaders
函数
在加载prototype的
行之后,但在执行任何其他操作之前,请先包含此内容
// monkey-patch Prototype's Ajax request (the following is for Prototype 1.4!)
Ajax.Request.prototype.setRequestHeaders = function () {
var requestHeaders =
['X-Requested-With', 'XMLHttpRequest',
'X-Prototype-Version', Prototype.Version];
if (this.options.method == 'post') {
requestHeaders.push('Content-type',
'application/x-www-form-urlencoded');
/* Force "Connection: close" for Mozilla browsers to work around
* a bug where XMLHttpReqeuest sends an incorrect Content-length
* header. See Mozilla Bugzilla #246651.
*/
if (this.transport.overrideMimeType)
requestHeaders.push('Connection', 'close');
}
if (this.options.requestHeaders)
requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
for (var i = 0; i < requestHeaders.length; i += 2) {
if (requestHeaders[i].toLowerCase() === "connection") continue;
this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
}
};
//monkey patch Prototype的Ajax请求(以下是Prototype 1.4的请求!)
Ajax.Request.prototype.setRequestHeaders=函数(){
var请求头=
['X-request-With','XMLHttpRequest',
“X-Prototype-Version”,Prototype.Version];
if(this.options.method=='post'){
requestHeaders.push('Content-type',
‘application/x-www-form-urlencoded’;
/*强制Mozilla浏览器使用“连接:关闭”
*XMLHttpRequest发送不正确内容长度的错误
*标题。参见Mozilla Bugzilla#246651。
*/
if(this.transport.overrideMimeType)
push('Connection','close');
}
if(this.options.requestHeaders)
requestHeaders.push.apply(requestHeaders,this.options.requestHeaders);
对于(变量i=0;i
prototype.js(1.5+?)的更高版本只将连接头设置为非常旧的Firefox浏览器,因此此问题会随着升级而消失。什么是。等于(…)
?那当然不是Javascript…@epascarello不,不是。在说明书上给我看看。哦。。不管怎样-我想用修改后的版本覆盖实际的prototype.js
文件-怎么做?:)@TomalakString.prototype.equals=函数(X){返回this.toString()==X;}代码>它可以是有效的JavaScript。;)太糟糕了,这不是发布内容的复制品,OP正在询问如何修改原始代码。重新打开将完全@Override prototype的方法吗?从技术上讲,它将实际替换方法()。但实际上,yes.toLowerCase是一个javascript函数,因此它应该有开/闭括号。您可以执行与下面代码中提到的相同的连接检查if(requestHeaders[i].toLowerCase()!=“connection”){this.transport.setRequestHeader(requestHeaders[i],requestHeaders[i+1]);
}@jeewans感谢您发现了这个错误!
// monkey-patch Prototype's Ajax request (the following is for Prototype 1.4!)
Ajax.Request.prototype.setRequestHeaders = function () {
var requestHeaders =
['X-Requested-With', 'XMLHttpRequest',
'X-Prototype-Version', Prototype.Version];
if (this.options.method == 'post') {
requestHeaders.push('Content-type',
'application/x-www-form-urlencoded');
/* Force "Connection: close" for Mozilla browsers to work around
* a bug where XMLHttpReqeuest sends an incorrect Content-length
* header. See Mozilla Bugzilla #246651.
*/
if (this.transport.overrideMimeType)
requestHeaders.push('Connection', 'close');
}
if (this.options.requestHeaders)
requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
for (var i = 0; i < requestHeaders.length; i += 2) {
if (requestHeaders[i].toLowerCase() === "connection") continue;
this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
}
};