dqc2017 2018-02-05 02:36
浏览 400
已采纳

javascript - 请求的资源上没有“Access-Control-Allow-Origin”标头。 我不知道该怎么办

well, this message always showed on my console.. can someone help me to fix this out?.. btw this is ionic.. this is my controller.js

angular.module('directory.controllers', [])

.controller('EmployeeIndexCtrl', function ($scope, EmployeeService, $http) {

    $scope.searchKey = "";

    $scope.clearSearch = function () {
        $scope.searchKey = "";
        findAllEmployees();
    }

    $scope.search = function () {
        EmployeeService.findByName($scope.searchKey).then(function (employees) {
            $scope.employees = employees;
        });
    }
    var findAllEmployees = function() {
        $http({
          method: 'GET',
          url: 'https://miudr.000webhostapp.com'
        }).then(function (res){
          $scope.employees = res.data;
          console.log(res);
        })
    }
    findAllEmployees();

})

.controller('EmployeeDetailCtrl', function ($scope, $stateParams, EmployeeService) {
    EmployeeService.findById($stateParams.employeeId).then(function(employee) {
        $scope.employee = employee;
    });
})

.controller('EmployeeReportsCtrl', function ($scope, $stateParams, EmployeeService) {
    EmployeeService.findByManager($stateParams.employeeId).then(function(employees) {
        $scope.employees = employees;
    });
});

and this is my json.php

<?php
header("Content-type:application/json");

 $connection = mysqli_connect("localhost", "root", "", "miudr") or die("Error " . mysqli_error($connection));

 $sql = "select * from data";
 $result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));

 while ($row = mysqli_fetch_assoc($result)) {
     $data[] = $row;
 }

 echo json_encode($data, JSON_PRETTY_PRINT);

 mysqli_close($connection);
 ?>

php file is located on my hosting.. but anyway i hide my username and password as root and "".. i dont know everytime i try to show this json.. its not showing me the result.. but showing me No'Access blablabla'

btw sorry for my bad english

  • 写回答

1条回答 默认 最新

  • douhanzhuo6905 2018-02-05 02:47
    关注

    You have to allow your backend to accept the request. Something like this

    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
    

    This will accept from all due to "*". If you only wish to accept requests from your IP, replace * with your IP.

    Else you can consider using JSONP. Just add dataType: 'JSONP' to your $http object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?