【问题标题】:Problems connecting front-end app with the server将前端应用程序与服务器连接的问题
【发布时间】:2019-04-28 14:33:20
【问题描述】:

我有一个在 gh-pages https://yourweatherapp.github.io/yourweatherapp.github.io 中运行的 Angular 应用程序,它向它在主机中运行的 node.js 应用程序发出请求。

以前,我曾在网上查阅过类似How to allow CORS? 的其他信息,但解决方案对我不起作用

我已将 node.js 应用程序配置为以这种方式允许来自此来源的请求:

const corsMiddleware = cors({
    origin: [process.env.URL, 'https://yourweatherapp.github.io/yourweatherapp.github.io/login']
  })

  app.use(corsMiddleware)
  app.options('*', corsMiddleware) 

但浏览器不允许接收答案并登录。

我做错了什么?

【问题讨论】:

    标签: node.js angular cors github-pages


    【解决方案1】:
    'https://yourweatherapp.github.io/yourweatherapp.github.io/login'
    

    查看浏览器控制台中的错误消息。它会告诉你没有权限读取数据的来源,它不会是那个。

    来源不包括路径。所以这不是一个有效的来源。

    应该只有https://yourweatherapp.github.io

    【讨论】:

    • 我试过'yourweatherapp.github.io',但它不起作用。可能是 gh-pages 问题还是主机问题?
    • 您还需要有一个服务器来托管您请求的 URL。 wheatherapp.com:8080/api/login 只是没有连接。
    • 该 url 在 a2hosting 中运行,如果我收到邮递员的请求,它就可以工作
    • 我想我已经检测到了问题,但我仍然失败了。如果我从本地运行前端,我可以在服务器上获取后端方法,但如果我从 www.wheatherapp.com 尝试(对不起,我必须将其更改为天气)。即使我已将 CORS 权限更改为 const corsMiddleware = cors({ origin: [process.env.URL, 'wheatherapp.com:8080'] //origin: [process.env.URL, 'localhost:4200'] }) app.use(corsMiddleware) app.options('*', corsMiddleware)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2021-06-29
    相关资源
    最近更新 更多