gpt4 book ai didi

javascript - 发出 GET 请求时出错 "Failed to load resource: net::ERR_CONNECTION_REFUSED"

转载 作者:行者123 更新时间:2023-12-02 23:52:55 37 4
gpt4 key购买 nike

对此非常陌生,但我正在尝试测试一个小应用程序,该应用程序对本地主机服务器 http://localhost:3000/golfpool-standings 上的快速端点执行 GET 请求。并使用返回的 JSON 在 html 表中显示数据,但是当我尝试在脚本中使用时,我得到无法加载资源:net::ERR_CONNECTION_REFUSED我正在实时服务器 http://127.0.0.1:5500/golfApp/index.html 上进行测试即使在这里查看了类似的答案后,我也遇到了困难。任何帮助表示赞赏。我的第一个问题请温柔点。

我可以访问端点http://localhost:3000/golfpool-standings从 Chrome 浏览器返回的 JSON 显示在屏幕上,因此看来后端正在工作。 https://github.com/paulcullen79/golfApp

// index.js
const data = $.getJSON("http://localhost:3000/golfpool-
standings")

// server.js
const express = require('express')
const app = express()

app.get('/golfpool-standings', (req, res) => {
res.send(data)
})

const port = process.env.port || 3000
app.listen(port, () => console.log(`Listening on port ${port}...`))

最佳答案

此行 $.getJSON("http://localhost:3000/golfpool-stands") 正在请求 json 资源,但您的服务器未提供服务json.

来自 jQuery 文档:

As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently.

更改您的服务器代码:

app.get('/golfpool-standings', (req, res) => {
// send data as json
res.json(data)
})

或者将您的请求更改为 $.get("http://localhost:3000/golfpool-stands")

关于javascript - 发出 GET 请求时出错 "Failed to load resource: net::ERR_CONNECTION_REFUSED",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55562837/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com