![](/img/trans.png)
[英]Is it possible send my object with methods (function) from server to client?
[英]Send a variable from my server to my client
當我console.log(response.data)它是未定義的。 這是為什么?
我已經嘗試過res.send和res.json,但是它們都返回undefined ...
我應該在客戶端還是在服務器中進行更改
服務器端
//server js file
app.get("/updateCart", (req, res) => {
//get items from cart
dataServiceAuth.findUser("joe")
.then((user) => {
res.json(user[0]); //I know user[0] has a value I console.log it to the screen
})
.catch((err) => {
console.log("ERROR. Can't find user")
});
});
客戶端
//client file
$("document").ready(function () {
fetch("/updateCart", {method: "GET"}).then(response => {
console.log(response.data);
});
});
app.get('/getItems' (req, res) => {
let x = [5, 4, 3, 2, 1];
res.send(x)
})
要么
app.get('/getItems' (req, res) => {
let x = [5, 4, 3, 2, 1];
res.json(x)
})
Client.js
var myItems;
fetch("/getItems", method: "GET").then(response => {
myItems = response.data;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.