【问题标题】:Node.js Express.js Send Jquery Ajax Post throws Constant Error - I'm stuckNode.js Express.js 发送 Jquery Ajax Post 抛出常量错误 - 我被卡住了
【发布时间】:2021-05-04 11:34:06
【问题描述】:

我正在尝试从 Node.js 到 Ajax 的非常简单的返回。我正在使用 Express.js 来实现这一点。

为了编写和管理 JSON 文件,我有上一条路线中的代码,它工作得很好。

问题来自第二条路线,当我尝试使用 JQuery ajax 成功方法使其返回某些内容时,它总是抛出错误。这是我发现的最简单的发送形式,但仍然不起作用。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test 2</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
    Cod: <input type="text" name="cod" id="cod">
    Text: <input type="text" name="text" id="text">
    <button id="boton">PRUEBA</button>
    <button id="getText">GET TEXT</button>
    
    <script>
        $('#boton').click(function() {
            let cod=$('#cod').val();
            let text=$('#text').val();
            console.log(cod);
            console.log(text);
            $.ajax({
                type: 'POST',
                data: {
                    cod: cod,
                    text: text,
                },
                url: 'http://localhost:1337/prueba'
            });
        });
        $('#getText').click(function() {
            $.ajax({
                type: 'POST',
                data: {

                },
                success: (result) => {
                    console.log(result);
                },
                error: (result) => {
                    console.log('ERROR');
                },
                url: 'http://localhost:1337/getText'
            });
        });
    </script>
</body>
</html>

app.js

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/prueba', function(req, res) {
    // ... First Route. The code here works just fine so I don't add it here.
});

app.post('/getText', function(req, res) {
    let text = "Hola mundo";
    console.log(text);

    res.send(text);
});

app.listen(1337, () => console.log('Started server at http://localhost:1337!'));

当您单击“获取文本”按钮时,它会进入 app.js 中的“/getText”路由,并且控制台会在 node.js 终端上记录文本,所以这不是问题。

问题似乎来自 res.send() 函数。

在浏览器控制台中,它会记录“错误”,因为它出现在与“获取文本”按钮关联的 ajax 错误方法中。我找不到让它返回成功的方法。

谁能帮帮我?我非常坚持这个项目的工作。

【问题讨论】:

    标签: javascript jquery node.js ajax express


    【解决方案1】:

    看起来像 CORS 问题...您是否从同一位置提供 index.html?

    它可以从节点服务 index.html

    const express = require('express');
    const bodyParser = require('body-parser');
    
    const app = express();
    app.use(bodyParser.urlencoded({ extended: true }));
    
    app.post('/prueba', function(req, res) {
        // ... First Route. The code here works just fine so I don't add it here.
    });
    
    app.post('/getText', function(req, res) {
        let text = "Hola mundo";
        console.log(text);
    
        res.send(text);
    });
    
    app.get('*', function (req, res) {   
        res.sendFile('index.html', {root: '.'})
       })
    
    app.listen(1337, () => console.log('Started server at http://localhost:1337!'));
    

    node/express 有几个选项,例如express middleware

    【讨论】:

    • 是的 index.html 在同一个位置。
    • 添加了完整的示例。在我的机器上工作;-)
    • 我执行了“npm install cors”,并且成功了。谢谢!!!
    猜你喜欢
    • 2013-04-17
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 2011-02-12
    • 2015-01-16
    • 2016-03-07
    • 2012-07-23
    相关资源
    最近更新 更多