【问题标题】:Client Html cannot post to api ERR_CONNECTION_TIMED_OUT客户端 Html 无法发布到 api ERR_CONNECTION_TIMED_OUT
【发布时间】:2020-05-27 20:00:33
【问题描述】:

我的 api 没有将数据从客户端代码获取到我在端口 3000 上的网络服务器的 localhost 上创建的 /api 端点。如何获取它以获取服务器端代码以获取发布请求和控制台。日志(请求);如果它是正确的Page started (index):23 POST https://goldengates.club:3000/api net::ERR_CONNECTION_TIMED_OUT (anonymous) @ (index):23 goldengates.club/:1 Uncaught (in promise) TypeError: Failed to fetch

客户端代码:

<!DOCTYPE html>

<html>
    <head>

    </head>
    <body>
        <script>
        console.log("Page started");
            const num1=1;
            const num2=2;
            const data=35;//{num1,num2};
            const options =
            {
                method: 'POST',
                headers:
                {
                    "Content-Type": "application/json"
                },

                body: JSON.stringify(data)
            };
            fetch('https://goldengates.club:3000/api'/*'https://goldengates.club/Cypher-Network/fetchTest.js'*/,options);
        </script>
    </body>
</html>

服务器端代码:

const express = require('express');
const app = express();
app.listen(3000,()=>console.log('listening on port 3000'));
app.use(express.static('public'));

app.post('/api',(request,response)=>
{
    response.setHeader("Content-Type", "application/json");response.send({ statusCode:200, customMessage : 'Sucess', status : true })

    console.log(request);
    response.end();
});

【问题讨论】:

    标签: javascript node.js api http-post backend


    【解决方案1】:

    函数fetch返回一个promise,要使用它,你需要调用then方法,像这样:

    // backend code
    app.post('/example', (req, res) => res.json({ data: 'abc' }));
    
    // frontend code
    fetch('/example', { method: 'post' }).then(r => r.json()).then(d => alert(d.data));
    

    我认为您在代码中显示的方式只是存储如何计算 promise 结果的函数,而不是执行它并产生实际结果(获取数据并传递给警报)。使用Promise.then 方法,您可以使用Content-Type: application/json 标头和body: JSON.stringify(object) 的方式传递数据。

    【讨论】:

    • 你好,我试着按照你的建议做,但我的 req 参数没有定义。
    • 客户端console.log("Page started"); const num1=1; const num2=2; const data=35;//{num1,num2}; fetch('https://goldengates.club:3000/api', { method: 'post' }).then(r =&gt; r.json()).then(d =&gt; alert(d.data));
    • 服务器端代码const express = require('express'); const app = express(); app.listen(3000,()=&gt;console.log('listening on port 3000')); app.use(express.static('public')); /*app.post('/api',(request,response)=&gt; //{ response.setHeader("Content-Type", "application/json");response.send({ statusCode:200, customMessage : 'Sucess', status : true })*/ app.post('/api', (req, res) =&gt; res.json({ data: 'abc' })); console.log(req); response.end(); //});
    • 错误日志fetchTest.js:11 console.log(req); ^ ReferenceError: req is not defined at Object.&lt;anonymous&gt; (/home/main/public_html/Cypher-Network/fetchTest.js:11:17)
    • 是的 req 是未定义的,我假设你想从 app.post('/api' 打印它之前的对象,但你关闭了函数。应该是=&gt; { res.json 而不是=&gt; res.json。这样你的箭头函数中有多个语句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2016-12-08
    • 2015-08-19
    • 1970-01-01
    相关资源
    最近更新 更多