【发布时间】: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