Leg*_*geo 6 javascript node.js reactjs fetch-api
我用 NodeJs 制作了我的第一个 API。
如果我尝试从浏览器的 URL 访问资源,就会得到以下结果:
我成功地访问了 Postman 的帖子。现在我尝试通过图形站点设置一个小调用,但我无法获取数据。
这是我在传奇中尝试过的调用:
export const fetchWrapper = async url => {
var request = new Request({
url: url,
method: "GET"
});
await fetch(request)
.then(res => res.json())
.then(
result => {
return result;
},
error => {
return error;
}
);
};
Run Code Online (Sandbox Code Playgroud)
这就是传奇
export function* getPosts() {
const url = `http://localhost:8080/feed/posts`;
try {
const data = yield call(fetch(url), {method: 'GET'});
console.log('data',)
yield put(getPostsResponse({ data }));
} catch (e) {
console.log("error", e);
}
}
Run Code Online (Sandbox Code Playgroud)
这些是我在控制台中遇到的错误:
更新
正如评论中所建议的,这是我的节点代码:
控制器/feed.js
exports.getPosts = (req, res, next) => {
res.json({
posts: [
{ id: 1, title: "Titolo 1", description: "descrizione 1" },
{ id: 2, title: "Titolo 2", description: "descrizione 2" },
{ id: 3, title: "Titolo 3", description: "descrizione 3" }
]
});
};
exports.createPosts = (req, res, next) => {
const title = req.body.title;
const description = req.body.description;
const ID = 1234;
res.status(201).json({
message: "success operation",
post: {
id: ID,
title: title,
description: description
}
});
};
Run Code Online (Sandbox Code Playgroud)
路线/feed.js
const express = require("express");
const router = express.Router();
const feedController = require("../controllers/feed");
router.get("/post", feedController.getPosts);
router.post("/post", feedController.createPosts);
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
应用程序.js
const express = require('express');
const bodyParser = require("body-parser");
const feedRoute = require('./route/feed');
const app = express();
app.use(bodyParser.json()); //application json
app.use('/feed', feedRoute);
app.listen(8080);
Run Code Online (Sandbox Code Playgroud)
更新
useEffect(() => {
// getPosts();
fetch("http://localhost:8080/feed/post")
.then(resp => resp.json())
.then(data => console.log('data', data));
}, [getPosts]);
Run Code Online (Sandbox Code Playgroud)
也尝试过这个,但什么也没有,我收到同样的错误。
预期行为:
我必须成功调用本地主机服务器。
解决方案
正如 @ivani 建议我刚刚启用 CORS,这是我添加到 app.js 的代码。这不是最好的解决方案,但现在我可以看到响应。
const allowedOrigins = ["http://localhost:3000", "http://localhost:8080"];
app.use(
cors({
origin: function(origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg =
"The CORS policy for this site does not " +
"allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
}
})
);
Run Code Online (Sandbox Code Playgroud)
正如 ivani 所建议的,我刚刚启用了 CORS。
我将其添加到nodeJs的App.js中
const allowedOrigins = ["http://localhost:3000","http://localhost:8080"];
app.use(
cors({
origin: function(origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg =
"The CORS policy for this site does not " +
"allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
}
})
);
Run Code Online (Sandbox Code Playgroud)
现在我可以获取数据了
这是整个 App.js 文件
const express = require('express');
const bodyParser = require("body-parser");
const feedRoute = require('./route/feed');
const cors = require("cors");
const app = express();
const allowedOrigins = ["http://localhost:3000", "http://localhost:8080"];
app.use(
cors({
origin: function(origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg =
"The CORS policy for this site does not " +
"allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
}
})
);
app.use(bodyParser.json()); //application json
app.use('/feed', feedRoute);
app.listen(8080);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14673 次 |
| 最近记录: |