【问题标题】:Multiple issues in firefox but works properly in chrome with no single issuefirefox 中的多个问题,但在 chrome 中正常工作,没有一个问题
【发布时间】:2016-04-26 18:49:35
【问题描述】:

我在使用 Firefox 时遇到了多个问题,但在 chrome 中没有。

1) TypeError 中的问题:response.body 为空。

2) 上传图片时出现 TypeError: Argument 1 of FormData.constructor does not implement interface HTMLFormElement.

显示问题的代码是

对于 1 号问题(我为此使用了 superagent)

  search( query='Ganesh Chowk' ){
    let url = "/api/v1/rental/?place__startswith="+encodeURIComponent(query);
    Request.get(url).then((response) => {
    if (response) {
        this.setState({
            place:response.body.objects,
        });
    } else {
        this.setState({
            place: [],
        });
    }
});

第二期

  let image = [];
 class RenderPhotos extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.state = {
            files: []
        };
    }

    onDrop(files) {
      console.log('Received files: ', files);
      this.setState({
          files: files
      });

    image = new FormData(files);
    $.each(files,function(i,file){
      image.append('image',file);
    });

    } 

为什么这段代码在 chrome 上运行而不在 Firefox 上运行?要使此代码在所有浏览器中运行,应该进行哪些更改?

【问题讨论】:

    标签: javascript ajax reactjs form-data superagent


    【解决方案1】:

    关于错误 2:如果将 parameter 传递给 FormData 构造函数,则它必须是 HTML <Form> 元素,而您的代码中似乎并非如此。所以只需删除参数:

    image = new FormData();
    

    更新:

    关于错误 1:正如您自己发现的那样,请求需要一个接受标头 application/json。由于 Chrome 和 Firefox 默认接受标头的差异,服务器似乎为 Chrome 而不是 Firefox 返回一个 json 有效负载。无论如何,显式标头解决了该问题。

    【讨论】:

    • 它就像一个魅力。你知道第一个问题的原因吗?
    • @米兰没有。在 Chrome 和 Firefox 中运行时,如果您在网络开发工具中比较 HTTP 请求和响应,也许您可​​以发现原因。
    • 谢谢,我会诊断问题。
    • 我将我的标题设置为 set('Accept', 'application/json') 并且它在 firefox 中工作,但不知道为什么没有这个 firefox 会引发错误。您能否将其发布到问题 1,以便我可以将其标记为已回答。
    • @milan 很棒。将您的发现添加到答案中
    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2016-07-13
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多