JQuery表单插件向Node.js发送空数据?

时间:2014-04-25 14:15:04

标签: javascript jquery node.js express

html表格:

<form id="importForm" enctype="multipart/form-data">

                        <p>
                            <label for="ownerName">Owner Name<pow
                                    class="requiredFormItem">&nbsp;*</pow></label><select id="ownerName"
                                name="ownerName"></select>
                        </p>
                        <p>
                            <label for="policyName">Policy Name<pow
                                    class="requiredFormItem">&nbsp;*</pow></label><select id="policyName"
                                name="policyName"></select>
                        </p>
                        <p>
                            <label for="partition">Custom Partition Name</label> <input
                                type="text" name="partition" id="partition" class="textField" />
                        </p>
                        <p>
                            <pow class="requiredFormItem">*&nbsp;</pow>
                            Required Fields
                        </p>
                        <p>
                            <input type="submit" id="importCampaignButton" class="submit"
                                value="Import" />
                        </p>
                    </form>

的javascript:

function importObject(){
var options = {
        target: '#importForm',
        type: 'POST',
        url: hostServer + '/import',
        beforeSubmit: ShowRequest,
        success: function () {
            displayMessageDialog("File imported successfully", true);
        },
        error: function(e){
            displayMessageDialog("An AJAX error occured. " + JSON.stringify(e));
        }
};
$('#importForm').ajaxForm(options);
//$('#importForm').ajaxSubmit(options);
}

function ShowRequest(formData, jqForm, options) {
var queryString = $.param(formData);
alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
return true;
}

$(function() {
    importObject();
});

点击提交时告诉我:

 About to submit: ownerName=161&policyName=Global+Policy&partition=

在node.js上:

我在网上收听帖子......

app.post('/import', campaigns.importCampaign2);

并打印......

exports.importCampaign2 = function (request, response){
utilities.debugLog('Import campaign...');
utilities.debugLog('request files? ' + request.files);
utilities.debugLog('request body? ' + JSON.stringify(request.body));
utilities.debugLog('request params? ' + JSON.stringify(request.params));
utilities.debugLog('request query? ' + JSON.stringify(request.query));

但我好像什么也没得到......

request files? undefined
request body? {}
request params? {}
request query? {}

我试图制作一个带文件上传的表单,但它必须与IE 9兼容,所以我不能使用html5。如果我执行ajaxSubmit(options),JQuery表单插件可以工作,但是返回的文件是......一个字符串&#39; [file Object]&#39;和一个身体参数,而不是文件参数...推测是HAD发送的形式,所以我试着用另一种方式希望它会发送实际文件。但它不再发送任何东西......

app.js设置:

app.set('port', process.env.PORT || 3333);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', {layout: false});
app.use(require('morgan')('dev'));
app.use(require('body-parser')({ keepExtensions: true}));
app.use(require('cookie-parser')());
app.use(require('express-session')({ secret: 'mySecret', store: new MemoryStore({ reapInterval: 600000}) }));
app.use(require('method-override')());
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    next();
});
app.use(express.static(path.join(__dirname, '/public')));
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
next();
});

@mscdex有趣!它发送了一些东西!禁用身体解析器将是一个真正的麻烦,但有了它我看到了我的价值观:

------WebKitFormBoundaryJo01BR3yUDBh094k
Content-Disposition: form-data; name="ownerName"

161
------WebKitFormBoundaryJo01BR3yUDBh094k
Content-Disposition: form-data; name="policyName"

Global Policy
------WebKitFormBoundaryJo01BR3yUDBh094k
Content-Disposition: form-data; name="partition"


------WebKitFormBoundaryJo01BR3yUDBh094k--

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

现在需要对Express 4使用多部分解析模块进行多部分/表单数据请求。此类模块的一些示例包括:busboy(/ connect-busboy / multer / { {3}})和reformed

原因是在Express 4中,bodyParser()中间件被提取到自己的模块(formidable)中,但是新模块现在只支持application / json和application / x-www-form-urlencoded,所以您必须单独使用另一个模块进行多部分/表单数据处理。