NodeJS Ajax请求失败

时间:2017-05-16 10:05:45

标签: node.js ajax express

我有一个表单,我试图从客户端提交到Express,但每次出现错误时都会:

  

无法POST / request_method

以下是我正在尝试的代码:

  

的index.html

<form id="wizard-content" method="post">
   <label>File</label>
   <input type="file" name="some" id="rsome">
   <label>Value</label>
   <input type="text" name="valSome" id="perfect">
</form>
<button type="submit" id="submit_form">Finish</button>
    <script type="text/javascript">
        jQuery('#submit_form').click(function() {
            if (jQuery(this).text().toLowerCase() === "finish") {
                submitForm();
            }
        });
        var submitForm = function(){
            var formData = {
                    'perfect'        : $('#perfect').val(),
                    'rsome'            : $('#rsome')[0].files[0]
                };
            if(formData){
                $.ajax({
                    url : '/request_method',
                    type : 'POST',
                    data : formData,
                    contentType : false,
                    cache : false,
                    processData: false,
                    success : function(response){
                        console.log(response);
                    }, 
                    error : function(error){
                        console.log(error);
                    }
                });
            }
        }
    </script>

在expressJs中:

  

server.js

var express     = require('express');
var bodyParser  = require('body-parser')
app.use(bodyParser());
.
.
router.post('/request_method', function(req, res){
   console.log(req.body);
   console.log(req.ip);
});

4 个答案:

答案 0 :(得分:2)

您的 myconnection() 'open connection sql = "UPDATE tblbankposting SET BankEntryDate=@date,@BankName=@name,BankCode=@code,AccountNo=@acc" sql &= ",TransactionType=@type,Amount=@amount,BankCharges=@charges,@ReferenceNo=@ref,Description=@des " sql &= "WHERE TransactionID=@id" command = New OleDbCommand(sql, connection) With command .Parameters.Add(New OleDbParameter("@id", OleDbType.VarChar, 30, "TransactionID")) .Parameters.Add(New OleDbParameter("@date", OleDbType.DBDate, 30, "BankEntryDate")) .Parameters.Add(New OleDbParameter("@name", OleDbType.VarChar, 30, "BankName")) .Parameters.Add(New OleDbParameter("@code", OleDbType.VarChar, 30, "BankCode")) .Parameters.Add(New OleDbParameter("@acc", OleDbType.VarChar, 30, "AccountNo")) .Parameters.Add(New OleDbParameter("@type", OleDbType.VarChar, 30, "TransactionType")) .Parameters.Add(New OleDbParameter("@amount", OleDbType.Double, 10, "Amount")) .Parameters.Add(New OleDbParameter("@charges", OleDbType.Double, 10, "BankCharges")) .Parameters.Add(New OleDbParameter("@ref", OleDbType.VarChar, 30, "ReferenceNo")) .Parameters.Add(New OleDbParameter("@des", OleDbType.VarChar, 30, "Description")) 'set parameter values .Parameters("@id").Value = txttxnid.Text .Parameters("@date").Value = CDate(dtpbankentrydate.Value.Date) .Parameters("@name").Value = cbobanks.Text .Parameters("@code").Value = txtbankcode.Text .Parameters("@acc").Value = cboaccno.Text .Parameters("@type").Value = cbotxntype.Text .Parameters("@amount").Value = CDbl(txtamount.Text) .Parameters("@charges").Value = CDbl(txtbankcharges.Text) .Parameters("@ref").Value = txtrefno.Text .Parameters("@des").Value = txtdescription.Text End With 可能未使用您已停止的app

两种解决方案

1 - 使用您的应用而不是路由器定义路线

router

2 - 或使用路由器

app.post('/request_method', function(req, res){ console.log(req.body); console.log(req.ip); }); 之前添加以下行,告诉您的应用程序使用router.post...中定义的路由。

router

答案 1 :(得分:0)

你应该像这样包含bodyParser,

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

答案 2 :(得分:0)

尝试:

var submitForm = function(){
    var formData = new FormData($('#wizard-content')[0]);
    if(formData){
        $.ajax({
            url : '/request_method',
            type : 'POST',
            data : formData,
            contentType : false,
            cache : false,
            processData: false,
            success : function(response){
                console.log(response);
            }, 
            error : function(error){
                console.log(error);
            }
        });
    }
}

在您的路线上,使用req.files[0]获取文件输入

答案 3 :(得分:-1)

我犯了一个非常愚蠢的错误,我忘记使用路由器添加URL。

app.use('/', router);