咕噜服务器:不支持CORS,给人错误 [英] Grunt Server: Not supporting CORS, gives error

查看:140
本文介绍了咕噜服务器:不支持CORS,给人错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器设置,一切都进行得很顺利,但我得到关于

I have a server setup and everything was going fine but i am getting errors about

   ....  is not allowed by Access-Control-Allow-Origin

这是既承载我的angularjs网站在端口9000和我的休息服务位于端口8678的咕噜服务器很奇怪的。

This is very strange as both the grunt server that hosts my angularjs site is on port 9000 and my rest service is on port 8678.

无论如何,我发现这个

  https://gist.github.com/Vp3n/5340891

这也解释了如何启用CORS繁重的服务器上,但我咕噜文件好好尝试一下看起来是一样的...这是我目前我的呼噜声文件的一部分

which explains how to enable CORS on the grunt server but my grunt file doens't look the same... this is my current part of my grunt file

  connect: {
      options: {
        port: 9000,
        // Change this to '0.0.0.0' to access the server from outside.
        hostname: 'localhost'
      },
      livereload: {
        options: {
          middleware: function (connect) {
            return [
              lrSnippet,
              mountFolder(connect, '.tmp'),
              mountFolder(connect, yeomanConfig.app)
            ];
          }
        }
      },
      test: {

任何帮助真的AP preciated

Any help really appreciated

在此先感谢

推荐答案

我不知道你所说的我的呼噜声文件看起来不一样的意思,但你需要阅读的咕噜,contrib-文档连接文档,它告诉你,中间件选项接受它应该返回中间件阵列功能。

I'm not sure what you mean by "my grunt file doesn't look the same", but you need to read the documentation of grunt-contrib-connect documentation which tells you that the middleware option accepts a function which should return an array of middlewares.

的简称要点是一个简单的中间件,它允许CORS。

The referred gist is a simple middleware which allows CORS.

在你的情况下,它看起来像:

In your case it would look like:

connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: 'localhost'
  },
  livereload: {
    options: {
      middleware: function (connect) {
        return [
          lrSnippet,
          mountFolder(connect, '.tmp'),
          mountFolder(connect, yeomanConfig.app),
          function(req, res, next) {
            res.setHeader('Access-Control-Allow-Origin', '*');
            res.setHeader('Access-Control-Allow-Methods', '*');
            next();
          }
        ];
      }
    }
  },
  test: {

这篇关于咕噜服务器:不支持CORS,给人错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆