【问题标题】:ajax net::ERR_EMPTY_RESPONSE after waiting for response for 2 mins - node.js server等待响应 2 分钟后的 ajax net::ERR_EMPTY_RESPONSE - node.js 服务器
【发布时间】:2018-02-05 04:43:56
【问题描述】:

我有这段代码,其中客户端通过 AJAX POST 请求将文件上传到服务器,然后服务器将该文件上传到云(cloudinary),并在上传完成后响应 AJAX 请求。

当文件上传时间超过2分钟时会出现问题(我计时,从请求开始直到错误发生)。

如果上传时间少于 2 分钟,则一切正常,而时间超过 2 分钟的上传在出错后完成,没有问题。但客户端在 2 分钟时收到空响应。

服务器端代码:

router.route('/posts').post(middleware.isLoggedIn, function (req, res) {
  var form = new multiparty.Form()
  form.parse(req, function (err, fields, files) {
    if (err) return err
    cloudinary.v2.uploader.upload(files.content[0].path, { resource_type: 
    'auto' }, function (err, result) {
      if (err) return err
      console.log(result)
      res.json({ result: result })
    })
})

客户端代码:

function newPost (type, title, content) {
  if (type === 'image') {
    $('#newImageForm').addClass('loading')
  } else if (type === 'video') {
    $('#newVideoForm').addClass('loading')
  } else if (type === 'gif') {
    $('#newGifForm').addClass('loading')
  }
  var form = new FormData()
  form.append('content', content)
  form.append('type', type)
  form.append('title', title)
  $.ajax({
    type: 'POST',
    url: '/posts',
    data: form,
    processData: false,
    contentType: false,
    timeout: 0,
    success: function (response) {
      if (type === 'image') {
        $('#newImageForm').removeClass('loading')
        $('#newImageForm').fadeOut()
        $('#imageTitle').val('')
        $('#image').val('')
      } else if (type === 'video') {
        $('#newVideoForm').removeClass('loading')
        $('#videoTitle').val('')
        $('#video').val('')
        $('#newVideoForm').fadeOut()
      } else if (type === 'gif') {
        $('#newGifForm').removeClass('loading')
        $('#gifTitle').val('')
        $('#gif').val('')
        $('#newGifForm').fadeOut()
      }
      successMessage(response._id)
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      errorMessage()
    }
  })
}

【问题讨论】:

    标签: javascript jquery node.js ajax httpresponse


    【解决方案1】:

    听起来您遇到了 nodejs 请求的内部超时。 https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback

    尝试使用req.setTimeout(10000); 将其设置为更高的值或使用req.setTimeout(0) 禁用它

    【讨论】:

      【解决方案2】:

      当您从 Ajax 发送需要很长时间的请求时,您必须做两件事...

      首先,您需要在 ajax 请求中添加超时,如下所示:

       $.ajax({
                          url: "YOUR URL",
                          type: 'POST',
                          data: { path: JSON.stringify(p) },
                          dataType: 'json',
                          timeout: 1000000,
      })
      

      但是,如果您的监听服务器没有监听长请求,那么仅此一项将不起作用,因此在节点中您执行以下操作:

      var server = app.listen(PORT, function() {
      
      });
      
      server.timeout = 600000; 
      

      这个 60000010 分钟,如果需要,您可以提供更长的价值...

      希望这能解决你的问题....

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-26
        • 2013-10-25
        • 2016-05-07
        • 1970-01-01
        • 1970-01-01
        • 2017-06-28
        • 2015-05-06
        相关资源
        最近更新 更多