最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ajax net::ERR_EMPTY_RESPONSE after waiting for response for 2 mins - node.js server - Stack Overflow

programmeradmin1浏览0评论

I have this code where the client uploads a file to the server through an AJAX POST request and then the server uploads that file to a cloud (cloudinary), and responds to the AJAX request after the upload is pleted.

The problem occurs when the file upload takes longer than 2 minutes ( i timed it, since the beginning of the request till the error occurs).

Everything is working fine if the upload takes less than 2 minutes, And uploads that take longer than 2 minutes are pleted after the error with no problem. but the client receives the empty response at the 2 minute mark.

server-side code:

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

Client-side code:

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()
    }
  })
}

I have this code where the client uploads a file to the server through an AJAX POST request and then the server uploads that file to a cloud (cloudinary), and responds to the AJAX request after the upload is pleted.

The problem occurs when the file upload takes longer than 2 minutes ( i timed it, since the beginning of the request till the error occurs).

Everything is working fine if the upload takes less than 2 minutes, And uploads that take longer than 2 minutes are pleted after the error with no problem. but the client receives the empty response at the 2 minute mark.

server-side code:

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

Client-side code:

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()
    }
  })
}
Share Improve this question asked Aug 27, 2017 at 23:05 madllamasmadllamas 5184 silver badges10 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

Sounds like you are running into the internal timeout of nodejs request. https://nodejs/api/http.html#http_request_settimeout_timeout_callback

Try setting it to something higher with req.setTimeout(10000); or disable it with req.setTimeout(0)

When you are sending your request which takes long time from Ajax you have to do 2 things...

first thing is that you need to do is add timeout in your ajax request like following:

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

But this alone would not work if your listening server is not listening long requests so in node you do following:

var server = app.listen(PORT, function() {

});

server.timeout = 600000; 

This 600000 is 10 minutes you can give longer value if required...

I hope this solves your problem....

发布评论

评论列表(0)

  1. 暂无评论