我有一个相对简单的 ajax 调用,它将文件从输入表单上传到服务器:
$(function(){
$("form").submit(function(){
var infile = $('#pickedfile');
var actFile = infile[0].files[0];
$.ajax(
{
type: "POST",
url: "http://localhost:3000/file_upload",
data: [
{
file: actFile
}],
dataType: 'text',
success: function ()
{
alert("Data Uploaded");
},
beforeSend: function ()
{
alert("Before Send");
return false;
},
error: function ()
{
alert("Error detected");
},
complete: function ()
{
alert("Completed");
}
});
});
});
我有一个成功接收文件的 node.js 服务器,并返回报告:
var express = require('express'),
wines = require('./routes/testscripts');
var app = express();
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
});
app.post('/file_upload', function(req, res) {
//file should be in req.files.pickedfile
// get the temporary location of the file
// undefined if file was not uploaded
var tmp_path = req.files.pickedfile.path;
res.send("Hello, this is server");
});
});
app.listen(3000);
该文件已成功上传,并且在服务器端似乎一切正常。
但是,在前端,没有任何警报触发,因此我无法响应来自服务器的任何新数据。我还缺少一些额外的步骤吗?