【问题标题】:How do I get around the error TypeError: self._form.on is not a function?如何解决错误 TypeError: self._form.on is not a function?
【发布时间】:2018-08-06 11:20:34
【问题描述】:

我正在使用节点 js 请求模块向网站发送发布请求。查看网站请求后,它使用“表单数据”作为有效负载。页面的内容类型是“application/x-www-form-urlencoded”。我的脚本正在电子应用程序中单击按钮运行。我的代码示例如下:

let options2 = {
                    method: "POST",
                    uri: site_url,//predefined
                    followAllRedirects: true,
                    headers: {
                        'Origin': checkout_host,//predefined
                        'Content-Type': 'application/x-www-form-urlencoded',
                        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                        'Accept-Language': 'en-US,en;q=0.8',
                        'Referer': referer//predefined
                        'User-Agent': UA//predefined
                    },
                    formData: {
                        information that site is requesting
                    }
request(options2, (err, resp, body) => }
        *I end up repeating the code for different information here*
       }

在运行我的代码时,这是我遇到的错误,它立即停止:

TypeError: self._form.on is not a function

【问题讨论】:

  • 如果将“Content-Type”标头设置为“multipart/form-data”会怎样?
  • @Partik 当我这样做时,我得到了同样的错误。
  • 你在 Node 中使用request 模块吗?还是在前端?
  • @Partik 节点中的请求模块

标签: javascript node.js reactjs request electron


【解决方案1】:

发生这种情况的原因似乎是在处理错误时,request 模块在FormData 对象上使用了.on 方法。但是,这不是浏览器 FormData,而是从另一个依赖项中提取的。

Node 可以很好地识别所有内容,但浏览器有一些问题。我假设由于您将其用于 Electron 应用程序,因此这可能是问题所在。

查看此内容是否有帮助:https://github.com/request/request/issues/1961#issuecomment-233956542

否则,如果这是在前端,则使用fetch() 可能是一种警报。 https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch


另一个hacky选项是进入request模块(特别是request.js)并搜索self._form.on并注释掉该块。但我完全不推荐这样做,因为这只是一种不好的做法,并不能真正解决问题,而且可能会导致意想不到的副作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2018-09-01
    • 2018-11-14
    • 2021-07-02
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多