Jquery JS如何向我的express服务器发送http get请求?

Jquery JS如何向我的express服务器发送http get请求?,jquery,express,xmlhttprequest,Jquery,Express,Xmlhttprequest,我正在尝试向使用js express编写的简单服务器发送http get请求。问题是我不知道应该在url中输入什么来连接到本地运行的服务器。 第二个问题如何从get请求中获取值? 在本例中,“something”值发送到请求中 服务器 const express = require('express'); const app = express(); const PORT = 4001; app.get("/question",(req, res, next) => { res.s

我正在尝试向使用js express编写的简单服务器发送http get请求。问题是我不知道应该在url中输入什么来连接到本地运行的服务器。
第二个问题如何从get请求中获取值? 在本例中,“something”值发送到请求中

服务器

const express = require('express');
const app = express();
const PORT = 4001;
app.get("/question",(req, res, next) => {
    res.send("something")
    console.log("got something")
})
app.listen(PORT, () =>{
    console.log("working")
})
客户

$(document).ready(function () {
    $('.item').on('click', (element) =>{
        $.get(??????/question)
    })
})

由于客户端不是express应用程序的一部分,并且假设您已运行开发模式

http://localhost:4001/question

CORS发行

您可能会遇到cors,因为客户端不是express应用程序的一部分

因此,您必须在express应用程序中包含以下代码

const cors = require('cors'); // npm i -s cors - to install it in your express app
app.use(cors());

要过滤源代码并执行更多操作,请参考

最有可能的
localhost:4001
。您的本地计算机通常由
localhost
或IP
127.0.0.1
@Taplar引用。现在我遇到了这样一个错误:从源站“”访问“localhost:4001/cos”处的XMLHttpRequest已被CORS策略阻止:跨源站请求仅支持协议方案:http、数据、chrome、chrome扩展、https。我能做点什么吗?可能是@Taplar的重复,我按照他们的建议做了(前面添加了http://),现在我遇到了这样一个错误:GET net::ERR\u CONNECTION\u被拒绝