如何进入Ajax调用的成功函数?

时间:2016-09-07 05:10:58

标签: javascript jquery ajax node.js

$.ajax({
          url: '/upload', 
          type: 'POST',
          data:formData,
          processData:false,
          contentType:false,
          error: function(jXhr, status){
              console.log('error: '+status);  
          },
          success: function(data){
              console.log('upload successful: '+data);
          }
      })

嘿,我正在编写一个应用程序来查找用户上传文件的文件大小,我想通过在上面的ajax调用的success函数中注销语句data.size来做到这一点。我知道ajax调用是成功的,因为在下面的server.js文件中,post请求在ajax调用return上被触发。但是,ajax调用似乎没有进入成功方法,因为没有从中记录任何语句,但是也没有从ajax调用返回错误。我该如何解决?

注意:我知道有关于同一问题的其他问题,我已尝试实施那里提供的解决方案,但它似乎无法正常工作

var express = require('express');
var formidable = require('formidable');
var multer = require('multer');
var path = require('path');
var upload = multer({dest: './uploads'});
var fs = require('fs');

var app = express();

var PORT = 8080;

app.use(express.static(__dirname+'/views'));

app.set('views', './views');

app.set('view engine', 'jade');

app.get('/', function(req, res){
   res.render('index.jade'); 
});

app.post('/upload', upload.single('Upload'), function(req, res){
  // create an incoming form object
  var form = new formidable.IncomingForm();

  // store all uploads in the /uploads directory
  form.uploadDir = path.join(__dirname, '/uploads');

  // every time a file has been uploaded successfully,
  // rename it to it's orignal name
  form.on('file', function(field, file) {
    fs.rename(file.path, path.join(form.uploadDir, file.name));
  });

  // log any errors that occur
  form.on('error', function(err) {
    console.log('An error has occured: \n' + err);
  });

  // once all the files have been uploaded, send a response to the client
  form.on('end', function() {
    res.end('success');
  });

  // parse the incoming request containing the form data
  var data = form.parse(req);
  //console.log(data);

});

app.listen(PORT, function(){
    console.log('Express listening on port: '+PORT);
});

0 个答案:

没有答案