Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在离子加速器中使用CORS_Angularjs_Ionic Framework_Cors_Httpservice - Fatal编程技术网

Angularjs 如何在离子加速器中使用CORS

Angularjs 如何在离子加速器中使用CORS,angularjs,ionic-framework,cors,httpservice,Angularjs,Ionic Framework,Cors,Httpservice,我正在尝试使用ionic http服务执行get请求。但我得到了一个交叉原点错误: XMLHttpRequest cannot load http://127.0.0.1/webService/getBlogs.php. Origin http://localhost is not allowed by Access-Control-Allow-Origin 我一直在寻找解决办法,但由于我的理解力有限,我迷失了方向,不知道该怎么办。 有人说,转到爱奥尼亚项目,将代理更改为完整的url。 有人说

我正在尝试使用ionic http服务执行get请求。但我得到了一个交叉原点错误:

XMLHttpRequest cannot load http://127.0.0.1/webService/getBlogs.php. Origin http://localhost is not allowed by Access-Control-Allow-Origin
我一直在寻找解决办法,但由于我的理解力有限,我迷失了方向,不知道该怎么办。 有人说,转到爱奥尼亚项目,将代理更改为完整的url。 有人说在NodeJS服务器中更改一些代码,我不知道在哪个文件和哪里可以找到它

有人可以指导我如何设置CORS以及在哪里设置标题信息吗

下面是我的代码:

var TMSApp = angular.module("TMSApp",["ionic"]);

TMSApp.service("TMSSvc",["$http","$rootScope",TMSSvc]);

TMSApp.controller("TMSCtrl",["$scope","$sce","$ionicLoading","TMSSvc",TMSCtrl]);

function TMSCtrl($scope, $sce,$ionicLoading,TMSSvc){

    $ionicLoading.show({
          template: 'Loading Blogs...'
        });
    $scope.blogs = [];
    $scope.$on("TMSApp.blogs",function(_, result){
        result.posts.forEach(function(b){
            $scope.blogs.push({
                name:b.author.name,
                title:$sce.trustAsHtml(b.title),
                avatar:b.author.avatar_URL
            });
        });
        $ionicLoading.hide();
    });
    TMSSvc.loadBlogs();
}

function TMSSvc($http,$rootScope){
    this.loadBlogs= function(){
        $http.get("http://localhost/IonicDemo/webService/getBlogs.php")
          .success(function(result){
              $rootScope.$broadcast("TMSApp.blogs",result);
          });
    }
}
和PHP web服务

<?php

require_once("conn.php");

class getBlogs{

    function __construct($db){
        $this->mysqli = $db->getLink();

    }

    function getAllBlogs(){
        header("HTTP/1.1 400 VALID REQUEST");
        header("Access-Control-Allow-Origin: *");
        header("content-type:application/json");
        $qry = "SELECT * FROM services";
        $res = $this->mysqli->query($qry);
        if(!$res){
          echo $this->mysqli->error;
        }
        $data = array();
        while($row = $res->fetch_assoc()){
            $data = $row;
            echo json_encode($data);
        }
        //$data;
        //echo $data;
    }
}

$gb = new getBlogs($db);
$gb->getAllBlogs();

?>

我猜您错过了后端的这一行。我不是php程序员,但这就是我在我的节点应用程序中所做的

“访问控制允许原点”http://localhost:8100“

在我的前端

myApp.config(function($stateProvider, $urlRouterProvider, $httpProvider) {

    // CORS

    $httpProvider.defaults.withCredentials = true;
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];

    // Ionic uses AngularUI Router which uses the concept of states
    // Learn more here: https://github.com/angular-ui/ui-router
    // Set up the various states which the app can be in.
    // Each state's controller can be found in controllers.js
    $stateProvider

    .state('initial', {
        url: "/initial",
        templateUrl: 'templates/initial.html',
        controller: 'initialController'

    });
});

你所说的节点内应用是什么意思?对于后端,我使用NodeJS。您使用PHP这可能会对您有所帮助