Angular.js否“访问控制允许来源”标头的请求的资源present。因此,原产地'空'是不允许访问 [英] Angular.js No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

查看:464
本文介绍了Angular.js否“访问控制允许来源”标头的请求的资源present。因此,原产地'空'是不允许访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //创建一个角模块。
VAR newsModule = angular.module('NewsModule',[]);//创建一个角控制器。
newsModule.controller('newsCtrl',['$范围,$ HTTP',函数($范围,$ HTTP){
//功能retrives POST,UPDATE,DELETE,GET数据$ http.defaults.headers.put = {
        访问控制允许来源':'*',
        访问控制允许的方法':'GET,POST,PUT,DELETE,OPTIONS,
        访问控制允许报头:内容类型,X-要求,凭借
        };
        $ http.defaults.useXDomain = TRUE;    $ scope.throughdata =功能(){ 删除$ http.defaults.headers.common ['X-要求-随着'];
         $ http.get('http://www.google.com')。然后(功能(数据错误){
            警报(数据);
            警报(错误);
            $ scope.days =数据。数据;
          });    }
}
 ]);

但我已经收到以下错误


  

XMLHtt prequest无法加载 http://www.google.com/ 。无访问控制允许来源标头的请求的资源present。因此,原产地'空'是不允许访问。



解决方案

访问控制允许来源 设置在从服务器的响应不是在客户端请求,以允许从不同来源的客户有机会获得响应。

在你的情况, http://www.google.com/ 不允许你的出身有机会获得响应。因此,您无法读取。

有关CORS的更多信息:<一href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS\">https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

//Create an Angular Module.
var newsModule = angular.module('NewsModule', []);

//Create an Angular Controller.
newsModule.controller('newsCtrl', ['$scope', '$http', function ($scope, $http) {
//function retrives POST,UPDATE,DELETE,GET data

$http.defaults.headers.put = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'
        };
        $http.defaults.useXDomain = true;

    $scope.throughdata = function (){

 delete $http.defaults.headers.common['X-Requested-With'];
         $http.get('http://www.google.com').then(function(data,error){
            alert(data);
            alert(error);
            $scope.days=data.data;
          });



    }
}
 ]);

But I have getting following errors

XMLHttpRequest cannot load http://www.google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

解决方案

Access-Control-Allow-Origin is set on the response from server, not on client request to allow clients from different origins to have access to the response.

In your case, http://www.google.com/ does not allow your origin to have access to the response. Therefore you cannot read it.

For more information about CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

这篇关于Angular.js否“访问控制允许来源”标头的请求的资源present。因此,原产地'空'是不允许访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆