将数据从JavaScript发送到node.js

将数据从JavaScript发送到node.js,javascript,node.js,Javascript,Node.js,下面是JavaScript代码。如何将玩家数组发送到node.js let players = []; for(var i=0; i<22; i++){ players.push($(".card > button").eq(i).attr("value")); } 我需要捕捉node.js中/play路径上的玩家数组。我该怎么做?是的,您可以将数据从浏览器Javascript发送到node.js应用程序。您可以使用Ajax调用并使用XM

下面是JavaScript代码。如何将玩家数组发送到node.js

let players = [];
for(var i=0; i<22; i++){
  players.push($(".card > button").eq(i).attr("value"));
}

我需要捕捉node.js中/play路径上的玩家数组。我该怎么做?

是的,您可以将数据从浏览器Javascript发送到node.js应用程序。您可以使用Ajax调用并使用XMLHttpRequestAPI或更现代的
fetch()
API发送数据。您可以在nodejs服务器中创建一个路由,例如
/play
,然后随请求一起发送数据。然后,服务器将需要解析传入数据(取决于数据的发送方式),然后才能对其进行操作

您还必须决定是否发送GET、POST或PUT请求(根据典型的REST设计和体系结构选择合适的请求)。如果这是开始一个游戏,并且您正在发送一组数据,那么您可能会使用POST请求并在请求体中以JSON的形式发送数据

在Express中,以下是您接收此类数据的方式:

app.use(express.json());
app.post("/play", (req, res) => {
    console.log(req.body);          // this would be the data sent with the request
    res.send("game started");
});
在浏览器中,以下是如何将一组播放器发送到服务器

fetch("/play", {
    method: "POST", 
    headers: {
       'Content-Type': 'application/json'
    },
    body: JSON.stringify(players)
}).then(response => {
    // this line of code depends upon what type of response you're expecting
    return response.text();     
}).then(result => {
    console.log(result);
}).catch(err => {
    console.log(err);
});

有关更多信息,请参阅MDN上的。

是的,您可以将数据从浏览器Javascript发送到node.js应用程序。您可以使用Ajax调用并使用XMLHttpRequestAPI或更现代的
fetch()
API发送数据。您可以在nodejs服务器中创建一个路由,例如
/play
,然后随请求一起发送数据。然后,服务器将需要解析传入数据(取决于数据的发送方式),然后才能对其进行操作

您还必须决定是否发送GET、POST或PUT请求(根据典型的REST设计和体系结构选择合适的请求)。如果这是开始一个游戏,并且您正在发送一组数据,那么您可能会使用POST请求并在请求体中以JSON的形式发送数据

在Express中,以下是您接收此类数据的方式:

app.use(express.json());
app.post("/play", (req, res) => {
    console.log(req.body);          // this would be the data sent with the request
    res.send("game started");
});
在浏览器中,以下是如何将一组播放器发送到服务器

fetch("/play", {
    method: "POST", 
    headers: {
       'Content-Type': 'application/json'
    },
    body: JSON.stringify(players)
}).then(response => {
    // this line of code depends upon what type of response you're expecting
    return response.text();     
}).then(result => {
    console.log(result);
}).catch(err => {
    console.log(err);
});

有关更多信息,请参阅MDN上的。

在客户端,您需要以下内容:

const postData = data => {
    const body = JSON.stringify(data);
    return fetch('https://your.url/play', {
        method: 'POST', // GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, cors, same-origin
        cache: 'no-cache', // default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, same-origin, omit
        headers: {
            'Content-Type': 'application/json',
        },
        redirect: 'follow', // manual, follow, error
        referrer: 'no-referrer', // no-referrer, client
        body
    })
        .then(response => response.json()) // parses JSON response into native JavaScript objects
}

const players = ['a', 'b', 'c'];

postData({data: players})
    .then(json => {
        console.log(json);
    })
    .catch(e => console.log(e));
app.use(express.json());
app.post("/play", (req, res) => {
    const players = req.body.data;
    ...
    ...
});
在服务器端,您需要以下内容:

const postData = data => {
    const body = JSON.stringify(data);
    return fetch('https://your.url/play', {
        method: 'POST', // GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, cors, same-origin
        cache: 'no-cache', // default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, same-origin, omit
        headers: {
            'Content-Type': 'application/json',
        },
        redirect: 'follow', // manual, follow, error
        referrer: 'no-referrer', // no-referrer, client
        body
    })
        .then(response => response.json()) // parses JSON response into native JavaScript objects
}

const players = ['a', 'b', 'c'];

postData({data: players})
    .then(json => {
        console.log(json);
    })
    .catch(e => console.log(e));
app.use(express.json());
app.post("/play", (req, res) => {
    const players = req.body.data;
    ...
    ...
});

在客户端,您需要以下内容:

const postData = data => {
    const body = JSON.stringify(data);
    return fetch('https://your.url/play', {
        method: 'POST', // GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, cors, same-origin
        cache: 'no-cache', // default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, same-origin, omit
        headers: {
            'Content-Type': 'application/json',
        },
        redirect: 'follow', // manual, follow, error
        referrer: 'no-referrer', // no-referrer, client
        body
    })
        .then(response => response.json()) // parses JSON response into native JavaScript objects
}

const players = ['a', 'b', 'c'];

postData({data: players})
    .then(json => {
        console.log(json);
    })
    .catch(e => console.log(e));
app.use(express.json());
app.post("/play", (req, res) => {
    const players = req.body.data;
    ...
    ...
});
在服务器端,您需要以下内容:

const postData = data => {
    const body = JSON.stringify(data);
    return fetch('https://your.url/play', {
        method: 'POST', // GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, cors, same-origin
        cache: 'no-cache', // default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, same-origin, omit
        headers: {
            'Content-Type': 'application/json',
        },
        redirect: 'follow', // manual, follow, error
        referrer: 'no-referrer', // no-referrer, client
        body
    })
        .then(response => response.json()) // parses JSON response into native JavaScript objects
}

const players = ['a', 'b', 'c'];

postData({data: players})
    .then(json => {
        console.log(json);
    })
    .catch(e => console.log(e));
app.use(express.json());
app.post("/play", (req, res) => {
    const players = req.body.data;
    ...
    ...
});

您将MDN中的一组内容复制到了您的
fetch()
调用中,这对于此处正在进行的操作是完全不必要的。您将MDN中的一组内容复制到了您的
fetch()
调用中,这一组内容对于此处正在进行的操作是完全不必要的。这应该是可接受的答案。我只能投赞成票:)这应该是公认的答案。我只能投赞成票:)