解决:angularjs has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

先上轮子:
https://stackoverflow.com/questions/20754489/access-control-allow-origin-and-angular-js-http

'use strict';

angular.module('myApp.view1', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/view1', {
            templateUrl: 'view1/view1.html',
            controller: 'View1Ctrl'
        });
    }])
    .controller('apart1', function ($scope) {
        $scope.name = "I am a partamter of app myApp.view3 controller aasdsadsadsd";
        $scope.userName = "请输入用户名称";
        $scope.password = "请输入用户密码";
    })
    .controller('View1Ctrl', function ($scope, $http) {
        $scope.userNamePlaceholder = "请输入用户名称";
        $scope.passwordPlaceholder = "请输入用户密码";
        $scope.userName = "";
        $scope.password = "";


        $scope.aStringInputPlaceholder = "请输入待加密字符串";
        $scope.aStringOutputPlaceholder = "这里输出加密后字符串";
        $scope.aStringInput = "";
        $scope.aStringOutput = "";

        $scope.requestApiForConverToBase64=function () {
            $http({
                method: 'GET',
                url: 'https://www.runoob.com/try/angularjs/data/sites.php',
                headers:{'Content-Type': 'application/json',
                    "Access-Control-Allow-Origin": "*",
                    'Accept': 'application/json'}
            }).then(function successCallback(response) {
                $scope.aStringOutput = response.data;
                console.log("--------1");
            }, function errorCallback(response) {
                // 请求失败执行代码
                console.log("--------2");
            });
        }
    });

核心解决办法:

headers:{'Content-Type': 'application/json',
"Access-Control-Allow-Origin": "*",
'Accept': 'application/json'}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容