簡體   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