【发布时间】:2017-08-03 04:58:43
【问题描述】:
我已经使用 Node.js Express http://localhost:3000/api/feeds 和 node.js 构建了一个 REST API,并填充了数据。
router.get('/api/feeds', function (req, res, next) {
documentDBConfig.getAllDocuments()
.then(() => res.json(documentDBConfig.feedsArray));
});
现在我制作了一个静态网站,并希望使用 javascript 或 jquery 从我的 REST API 获取数据。我用了这段代码
$.getJSON( "http://localhost:3000/api/feeds", function( data ) {
console.log(data);
});
但它一直在说
XMLHttpRequest cannot load http://localhost:3000/api/feeds. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
我知道我做错了,但我找不到正确的方法。如何从我的 REST API (http://localhost:3000/api/feeds) 获取我的网站的这个 json 内容?
编辑:我没有通过资源管理器收到此警告,但我无法获取内容。现在我解决了 chrome 问题,因此我不再收到此警告。但我无法阅读内容。这不是重复。
现在我收到了这个警告
Failed to load resource: the server responded with a status of 404 (Not Found)
【问题讨论】:
-
@Deividas Karžinauskas 是的,已经阅读了这个答案,但我做的其他的正确吗?我的意思是我可以用这个 jquery 函数读取一个 url 内容吗?
-
你的jquery函数不错。您需要更新服务器标头以允许来自其他 url 的请求。阅读帖子建议的资源。
-
@Deividas Karžinauskas 但我没有收到资源管理器的警告,我也无法获得 json 内容。
标签: javascript jquery json rest api