【问题标题】:Injecting custom network url not working relayjs注入自定义网络url不起作用relayjs
【发布时间】:2015-12-06 06:48:21
【问题描述】:

我正在关注这个示例应用程序:https://github.com/relayjs/relay-starter-kit

Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('http://localhost:8080/graphql')
);

使用自定义网址我收到以下错误

选项http://localhost:8080/graphql
net::ERR_EMPTY_RESPONSEself.fetch @relay.js:21558Promise @
app.js:3335self.fetch@relay.js:21508sendTimedRequest@
relay.js:20131module.exports@relay.js:20186tryCallTwo@
relay.js:3762doResolve @relay.js:3879Promise @
relay.js:3782fetchWithRetries@relay.js:20122_sendQuery@
relay.js:13042(匿名函数)@relay.js:12976sendQueries @
relay.js:12975sendQueries@relay.js:3502processQueue@
relay.js:17591(匿名函数)@relay.js:17582tryCallOne @
relay.js:3754(匿名函数)@relay.js:3820flush@relay.js:9457
relay.js:21558 选项http://localhost:8080/graphql
net::ERR_EMPTY_RESPONSEself.fetch @relay.js:21558Promise @
app.js:3335self.fetch@relay.js:21508sendTimedRequest@
relay.js:20131 relay.js:21558 选项http://localhost:8080/graphql
net::ERR_EMPTY_RESPONSEself.fetch @relay.js:21558Promise @
app.js:3335self.fetch@relay.js:21508sendTimedRequest@
relay.js:20131 relay.js:7449 网络请求失败_markAsRejected @
relay.js:7449_handleSubtractedQueryFailure @relay.js:7430tryCallOne @
relay.js:3754(匿名函数)@relay.js:3820flush@relay.js:9457

【问题讨论】:

    标签: relayjs


    【解决方案1】:

    我猜您的应用程序在端口 3000 上运行。如果您尝试向不同的域/端口组合发出客户端=>服务器请求,而不在服务器端启用此类跨域请求,您的浏览器将使用其默认的CORS 策略来阻止请求。

    您可以在服务器端启用来自所有来源的请求,如下所示:

    import cors from 'cors';
    
    var graphQLServer = express();
    graphQLServer.use(cors());
    /* ... */
    

    查看cors docs 以获得更细粒度的控制。

    请务必npm install --save cors,无论您的服务器代码位于何处。

    【讨论】:

    • 谢谢 :) ,不知道 cros 下的不同端口通信。
    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多