简体   繁体   English

从角度JS控制器启用CORS

[英]enabling CORS from controller of angular JS

I have a controller with service that would get the json from other server and i came to this 我有一个带有服务的控制器,该服务将从其他服务器获取json,我来到了这里

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:somesite.com. This can be fixed by moving the resource to the same domain or enabling CORS.

I notice that some of the same issue would be fix by "Access-Control-Allow-Origin": "*" in header my in my case I don't see any progress on fixing it. 我注意到某些相同的问题将通过标题中的"Access-Control-Allow-Origin": "*"进行修复"Access-Control-Allow-Origin": "*"在我的头文件中, "Access-Control-Allow-Origin": "*"我看不到任何修复问题。

this the code for my controller : 这是我的控制器的代码:

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

myApp.controller('GetItemCtrl', ['$scope', '$http', function($scope, $http) {

    var itemUrl = 'http:somesite.com';

    //console.log(itemUrl);
    $http({url: itemUrl,
    dataType: 'json',
    method: 'POST',
    dataType : 'json',
    headers: {
       "Access-Control-Allow-Origin": "*",
       "Access-Control-Allow-Methods": "POST, GET, OPTIONS, PATCH, DELETE",
       "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
    },
    xhrFields: {
        withCredentials: true
    }}).success(function(data, status, headers, config) {
        //check if the response has ok status
        if(data.status == "ok"){
        console.log(data.data);
            $scope.items = data.data;
        }


        }).error(function(data, status, headers, config) {


    });

}]);

I added the header but it seem not to fix my cross origin issue. 我添加了标题,但似乎无法解决我的跨源问题。 Could anyone drive me by any comments and suggestion on how am i able to get the response from another server source. 任何人都可以通过关于如何从另一个服务器源获得响应的任何评论和建议来驱使我。 thank you in advance. 先感谢您。

CORS is a server solution - that is - the first request a web client (JS) sends to a different domain (hence cross domain) is an OPTIONS request - which is replied by the origin server with weather or not it respect cross domain requests, if so it answers: CORS是一种服务器解决方案-也就是说-Web客户端(JS)发送到另一个域(因此是跨域)的第一个请求是OPTIONS请求-原始服务器会回复天气信息,或者它是否尊重跨域请求,如果是这样,它将回答:

   "Access-Control-Allow-Origin": "*",
   "Access-Control-Allow-Methods": "POST, GET, OPTIONS, PATCH, DELETE",
   "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"

Then the browser sends another request - the actual request that was meant to be sent. 然后,浏览器发送另一个请求-本应发送的实际请求。 This whole mechanism takes place automatically. 这整个机制是自动发生的。 All you have to do is enabling CORS on your server. 您要做的就是在服务器上启用CORS。 To enable CORS, here are some useful links: 要启用CORS,以下是一些有用的链接:
ASP.NET WEBAPI ASP.NET WEBAPI
APACHE 亚太地区

CORS is something that has to be enabled on the server . CORS是必须在服务器上启用的功能。 If you don't have access to that server you probably won't be able to do what you were hoping. 如果您无权访问该服务器,则可能无法执行您希望的操作。 If you do have access you'll need to read up on how to enable it for whatever language the api was written in :) 如果您具有访问权限,则需要阅读有关如何使用api编写的任何语言启用它的信息:)

See here for more info: CORS request is not allowed with AngularJS 查看更多信息: AngularJS不允许CORS请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM