【问题标题】:Prototype.js 1.4.0 throws 'Refused to set unsafe header "Connection"' ErrorPrototype.js 1.4.0 抛出“拒绝设置不安全的标头“连接””错误
【发布时间】:2021-04-24 21:41:48
【问题描述】:

我试图通过添加setRequestHeaders 函数if()continue;覆盖 prototype.js 以避免指定Header

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]);
  }

在 IE9 上,无需更改 prototype.js 即可正常工作,但在其他浏览器(Chrome、Firefox 等)中会出现标题错误..

感谢您的任何建议和帮助! :)

[编辑]

关于重复: 我找到了这个主题,但它不一样。在重复的主题中有一个可以修改的文件,但我无法修改prototype.js的代码。

[编辑2]

此代码在 1.4.0 中(如标题所示),大约 661-685 行

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){
    console.log(requestHeaders[i] + " AND " + requestHeaders[i+1]);
    //My edition - BEGIN;
    if("Connection" === requestHeaders[i] || "Connection" === requestHeaders[i+1])
            continue;
    //END;
  this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
  }
}

谢谢! :)

【问题讨论】:

  • .equals(...) 是什么?那肯定不是 Javascript...
  • @epascarello 不,不是。在规格中告诉我。
  • 哦.. 随便 - 我想用我的修改版本覆盖实际的 prototype.js 文件 - 怎么做? :)
  • @Tomalak String.prototype.equals = function(X){ return this.toString()===X; }; 它可以是有效的 JavaScript。 ;)
  • 太糟糕了,这不是所发布的内容,OP 正在询问如何更改原始代码。重新开放

标签: javascript


【解决方案1】:

因此,您似乎想在不触及库代码本身的情况下更改 Prototype 的行为。

没问题,您可以在运行时换掉setRequestHeaders 函数。

在加载原型的&lt;script&gt; 行之后,但在您执行任何其他操作之前,将其包含在内。

// 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]);
  }
};

【讨论】:

  • 这会完全@Override原型的方法吗?
  • 从技术上讲,它将物理替换方法 (see)。但实际上,是的。
  • toLowerCase 是一个javascript函数,因此它应该有开/关括号。您可以对以下代码中提到的连接进行相同的检查。 if (requestHeaders[i].toLowerCase() != "connection") { this.transport.setRequestHeader(requestHeaders[i],requestHeaders[i+1]); }
  • @Jeewantha 感谢您发现这个错字!
【解决方案2】:

prototype.js 的更高版本(1.5+?)仅将 Connection 标头设置为非常旧的 Firefox 浏览器,因此此问题随着升级而消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2016-01-13
    • 2014-07-07
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多