我正在尝试从Angularjs客户端对我的API进行POST,我在另一个域中运行的服务器上进行了此配置:
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS, DETELE');
res.setHeader('Access-Control-Allow-Headers', '*');
next();
});
Run Code Online (Sandbox Code Playgroud)
发送到服务器的标头是:
OPTIONS /api/authenticate HTTP/1.1
Host: xxxx.herokuapp.com
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://127.0.0.1:5757
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://127.0.0.1:5757/login
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en,es;q=0.8,gl;q=0.6,pt;q=0.4
Run Code Online (Sandbox Code Playgroud)
响应标头是:
HTTP/1.1 403 Forbidden
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DETELE
Access-Control-Allow-Headers: X-Requested-With,content-type,Authorization
Content-Type: application/json; charset=utf-8
Content-Length: …Run Code Online (Sandbox Code Playgroud) 有许多 Heroku CLI Postgres 命令都返回相同的错误。例如:
$ heroku pg:bloat
psql: FATAL: no pg_hba.conf entry for host "...", user "...", database "...", SSL off
Run Code Online (Sandbox Code Playgroud)
这些命令中至少有一个在过去有效,但现在无效。
否则数据库似乎工作正常。我可以通过我的应用程序的接口访问它。
我没有看到在 Heroku Postgres 仪表板中切换 SSL 的方法。什么可能导致这种情况,我该如何解决?
我尝试使用express.js CORS 包将 CORS 请求的来源限制为每个路由的一个特定域,如下所示:
const express = require('express');
const cors = require('cors');
const port = process.env.PORT || 3000;
let app = express();
app.get('/', cors({origin: 'http://example.com'}), (req, res, next) => {
res.sendStatus(200);
});
app.post('/', cors({origin: 'http://whatever.com'}) (req, res, next) => {
res.sendStatus(200);
});
app.listen(port, () => {
console.log(`Started on port ${port}`);
});
Run Code Online (Sandbox Code Playgroud)
然而,这似乎没有任何效果,因为我可以GET进出POST任何域。然后,我尝试使用以下命令将所有路由限制为一个来源,但得到了相同的结果:
app.use(cors({origin: 'http://example.com'}));
Run Code Online (Sandbox Code Playgroud)
我在 localhost 上的开发环境和 Heroku 上的生产环境中都遇到了这种情况。知道我缺少什么吗?
我无法理解Apollo Client库,因为它无法按预期工作.它不是发送GETHTTP方法,而是发送OPTIONSHTTP方法,即使我只在从GraphQL服务器检索数据时才使用GET.
const client = new ApolloClient({
link: ApolloLink.from([
new MeteorAccountsLink(),
new HttpLink({
uri: 'https://selo-comments.herokuapp.com/graphql',
useGETForQueries: true
})
]),
cache: new InMemoryCache()
});
Run Code Online (Sandbox Code Playgroud)
来自浏览器的控制台日志:
OPTIONS https://selo-comments.herokuapp.com/graphql?query=%7B%0A%20%20comments(id%3A%20%22TFpQmhrDxQqHk2ryy%22)%20%7B%0A%20%20%20%20articleID%0A%20%20%20%20content%0A%20%20%20%20userId%0A%20%20%20%20createdAt%0A%20%20%20%20commentID%0A%20%20%20%20votes%0A%20%20%20%20blockedUsers%0A%20%20%20%20__typename%0A%20%20%7D%0A%7D%0A&variables=%7B%7D 405 (Method Not Allowed)
这显然意味着HTTP方法不正确,即使它在url中有查询参数.如果您使用Postman查询该URL或只是使用浏览器的地址栏导航到该URL,您将获得GraphQL数据.我必须使用https://cors-anywhere.herokuapp.com/才能成功执行查询.
我究竟做错了什么?
cors ×2
heroku ×2
javascript ×2
node.js ×2
angularjs ×1
express ×1
graphql ×1
heroku-cli ×1
http ×1
http-headers ×1
meteor ×1
postgres-10 ×1
postgresql ×1