无“访问控制允许来源” - 节点/ Apache的端口号 [英] No 'Access-Control-Allow-Origin' - Node / Apache Port Issue

查看:160
本文介绍了无“访问控制允许来源” - 节点/ Apache的端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建使用节点/ EX preSS和尝试使用数据Angularjs但我的HTML页面下的Apache运行在localhost上拉一个很小的API:8888和节点API是监听3000端口,我得到否访问控制允许来源。我试着使用节点的HTTP代理服务器和虚拟主机的Apache,但没多少更迭,请参阅完整的错误及以下code。

XMLHtt prequest无法加载本地主机:3000无访问控制允许来源标头的请求的资源present产地的'localhost:8888'。因此不允许访问。

  // API中使用节点/ EX preSS
VAR前preSS =要求('前preSS');
VAR应用=前preSS();
VAR承包商= [
    {
     ID:1
        名:乔Blogg
        周:3,
        照片:1.png
    }
];app.use(如press.bodyParser());app.get('/',函数(REQ,RES){
  res.json(承包商);
});
app.listen(process.env.PORT || 3000);
的console.log('服务器正在端口3000上运行)
    //角code
    angular.module('contractorsApp',[])
    .controller('ContractorsCtrl',函数($范围,$ HTTP,$ routeParams){   $ http.get('本地主机:3000')。成功(功能(数据){    $ scope.contractors =数据;   })   // HTML
   <机身NG-应用=contractorsApp>
    < D​​IV NG控制器=ContractorsCtrl>
        < UL>
         <李NG重复=人承包> {{person.name}}< /李>
        < / UL>
    < / DIV>
   < /身体GT;


解决方案

尝试添加以下的中间件到你的NodeJS / EX preSS的应用程序(我已经添加了您的方便,一些评论)

  //添加标题
app.use(功能(REQ,资源,下一个){    //网站您希望允许连接
    res.setHeader('访问控制允许来源,HTTP://本地主机:8888');    要允许//请求方法
    res.setHeader(访问控制允许的方法,GET,POST,OPTIONS,PUT,PATCH,DELETE');    你想//请求头,让
    res.setHeader(访问控制允许报头','X-要求-着,内容类型');    //设置为true,如果你需要的网站,包括在发送的请求饼干
    //对API(例如,如果你使用会话)
    res.setHeader(访问控制允许的凭据',真);    //传递给中间件的下一层
    下一个();
});

希望帮助!

i've created a small API using Node/Express and trying to pull data using Angularjs but as my html page is running under apache on localhost:8888 and node API is listen on port 3000, i am getting the No 'Access-Control-Allow-Origin'. I tried using node-http-proxy and Vhosts Apache but not having much succes, please see full error and code below.

"XMLHttpRequest cannot load localhost:3000. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8888' is therefore not allowed access."

// Api Using Node/Express    
var express = require('express');
var app = express();
var contractors = [
    {   
     "id": "1", 
        "name": "Joe Blogg",
        "Weeks": 3,
        "Photo": "1.png"
    }
];

app.use(express.bodyParser());

app.get('/', function(req, res) {
  res.json(contractors);
});
app.listen(process.env.PORT || 3000);
console.log('Server is running on Port 3000')


    // Angular code
    angular.module('contractorsApp', [])
    .controller('ContractorsCtrl', function($scope, $http,$routeParams) {

   $http.get('localhost:3000').success(function(data) {

    $scope.contractors = data;

   })

   // HTML
   <body ng-app="contractorsApp">
    <div ng-controller="ContractorsCtrl"> 
        <ul>
         <li ng-repeat="person in contractors">{{person.name}}</li>
        </ul>
    </div>
   </body>

解决方案

Try adding the following middleware to your NodeJS/Express app (I have added some comments for your convenience):

// Add headers
app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8888');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});

Hope that helps!

这篇关于无“访问控制允许来源” - 节点/ Apache的端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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