【问题标题】:NodeJS Ajax request failsNodeJS Ajax 请求失败
【发布时间】:2017-05-16 18:23:48
【问题描述】:

我有一个表单我试图从客户端提交到 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);
});

【问题讨论】:

  • 把整个网址的名字..eg.localhost:5000/xxxx
  • 还有其他错误信息吗?响应是什么状态(例如检查 devtools)
  • @ovidiuDolha 我没有收到任何其他错误,这是一个错误,我收到 404 响应。
  • @sabareesh 我已经尝试使用 (window.location.origin+method_name) 仍然遇到同样的错误。

标签: node.js ajax express


【解决方案1】:

您的app 可能没有使用您已使用的router

两种解决方案

1 - 使用您的应用而非路由器定义路由

app.post('/request_method', function(req, res){
   console.log(req.body);
   console.log(req.ip);
});

2 - 或使用您的路由器

router.post... 之前添加以下行,这将告诉您的应用程序使用在您的router 中定义的路由。

app.use('/', router);

【讨论】:

  • 我犯了一个非常愚蠢的错误,我没有为这个特定的 URL 创建路由器。多谢,伙计。它奏效了。
  • 如果有帮助,请随时接受我的帖子作为答案!
【解决方案2】:

你应该像这样包含bodyParser,

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

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

【讨论】:

    【解决方案3】:

    试试看:

    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]获取文件输入

    【讨论】:

      【解决方案4】:

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

      app.use('/', router);
      

      【讨论】:

      • 如果有人已经给您满意的答案,您无需自己发布答案。只要接受他的回答就够了。 :)
      猜你喜欢
      • 2017-04-02
      • 2018-11-30
      • 2013-01-15
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2012-03-18
      相关资源
      最近更新 更多