【问题标题】:Angular js : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resourceAngular js:跨域请求被阻止:同源策略不允许读取远程资源
【发布时间】:2014-12-26 07:40:53
【问题描述】:

当我尝试在 Angular Js 中使用 $http.post() 发布数据时出现此错误。

跨域请求被阻止:同源策略不允许读取 {{post url}} 处的远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。

这是我的代码:

var ionicAppControllers = angular.module('ionicAppControllers', []);

ionicAppControllers.config(function($httpProvider) {
    //Enable cross domain calls
    $httpProvider.defaults.useXDomain = true;

    //Remove the header containing XMLHttpRequest used to identify ajax call
    //that would prevent CORS from working
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
});


ionicAppControllers.controller('createItemCtrl', ['$scope', '$http',
    function($scope, $http) {
        $scope.parts = [];

        $scope.add = function() {
            $scope.parts.push({
                id: '',
                code: '',
                description: ''                   
            });
        };
        $scope.submitItemForm = function(isValid) {
            if (isValid) {
                $scope.postdata = {};
                $scope.postdata.item = $scope.item;
                $scope.postdata.description = $scope.description;
                for (var i in $scope.parts)
                {
                    for (var j in $scope.parts[i])
                    {
                        if (j == '$$hashKey')
                        {
                            delete($scope.parts[i][j]);
                            //console.log(j);
                        }
                    }
                }
                $scope.postdata.parts = $scope.parts;
                console.log($scope.postdata);
                $http({
                    method: 'POST',
                    url: 'URL',
                    data: $scope.postdata, // pass in data as strings
                    headers: {'Content-Type': 'application/json'}  
                }).success(function(data) {
                    if (data.success)
                    {
                        alert('sucess');
                    }
                    else
                    {
                        alert('fail');
                    }
                });
            }
        };
        $scope.add();
    }]);

【问题讨论】:

  • 不知道是什么...只是为了消除错误而写...

标签: angularjs angularjs-directive angularjs-scope cors


【解决方案1】:

这与 Angular 无关。服务器应用程序应该启用或允许从托管或运行在任何机器上的 Angular 应用程序生成的请求。如果您在 localhost 中运行您的应用程序。对 chrome 使用 CORS 扩展。如果它来自其他服务器。让我们说 www.example.com 。

服务器代码必须从前端角度进行任何更改。

请参考此链接。 http://enable-cors.org/server.html

【讨论】:

    【解决方案2】:

    如果你使用 ASP MVC 并获得 jsonresult 你必须在 web.config 中添加这个 xml 代码

        <?xml version="1.0" encoding="utf-8"?>
    <configuration>
     <system.webServer>
       <httpProtocol>
         <customHeaders>
           <add name="Access-Control-Allow-Origin" value="*" />
         </customHeaders>
       </httpProtocol>
     </system.webServer>
    </configuration>
    

    【讨论】:

    • 他们有没有提到他们使用 ASP.NET MVC 的任何地方?
    猜你喜欢
    • 2018-10-02
    • 2021-04-04
    • 2014-10-17
    • 2015-07-22
    • 2018-04-20
    • 2014-07-20
    相关资源
    最近更新 更多