繁体   English   中英

请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许Origin'http:// localhost:8100'访问

[英]No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access

server.js我已经声明了权限

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.js

angular.module('app', ['ionic', 'ui.router'])

.config(('$urlRouterProvider', '$stateProvider', function($urlRouterProvider,$stateProvider){
    $urlRouterProvider.otherwise('/');
    $stateProvider
    .state('view',{
      url:'/',
      templateUrl:'js/components/view/view.html',
      controller:'viewCtrl'
    })

}))

view.js

.controller('viewCtrl',['$scope','$state','$http',function($scope,$state,$http)
{   

    $scope.pics=[];
    $http.get('http://localhost:8080/getNewPhoto').then(function(response){
        $scope.pics=response.data;
    },function(err)
    {
        console.error("There's an error"+err);
    })
}])

请帮助我解决这个问题我已经尝试了所有可能的解决方案,但问题仍然存在

只需使用快递的cors模块 ,您就可以了:

var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())

您必须添加一个重要的标题。 将该行添加到服务器脚本:

res.header('Access-Control-Allow-Methods','POST, GET, OPTIONS, PUT');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM