使用AngularJS的跨域请求不起作用

时间:2022-08-22 19:12:54

I need to do a Cross Domain Request using Angular, but I got the error

我需要使用Angular进行跨域请求,但是我收到了错误

XMLHttpRequest cannot load http://machine_name_in_my_network:8082/GetAll. No

'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:53379' is therefore not allowed access. The response had HTTP status code 500.

'access-Control-Allow-Origin'标头出现在请求的资源上。因此不允许来源'http:// localhost:53379'访问。响应的HTTP状态代码为500。

I saw here a solution sample, but doesn't worked for me.

我在这里看到了一个解决方案样本,但对我没有用。

This is a request from my localhost to a machine in my network and should return a JSON.

这是我的localhost对我网络中的计算机的请求,应该返回一个JSON。

Code Snippet

//Controller 
function crmContatosCtrl($scope, apiService) {
    apiService.get('/GetAll').then(function (data) {
       $scope.contatos = data;
    });

and then comes to my service

然后来我的服务

function comWebApi_Service($http, $log) {
    return {
        get: function (url, data) {

            //return $http.jsonp(ApiURL + url)
            //    .success(function (data) {
            //        debugger;
            //    })
            //    .error(function (data) {
            //        debugger;
            //    });

            return $http.get(ApiURL + url, data).then(
                function (res) {                
                    return res.data;
                });
        },

    angular
        .module('app')
        .config(['$httpProvider', function($httpProvider) {
            $httpProvider.defaults.useXDomain = true;
            delete $httpProvider.defaults.headers.common['X-Requested-With'];
        }])
        .service('apiService', comWebApi_Service);

2 个解决方案

#1


1  

you need to enable cors on server

你需要在服务器上启用cors

e.g. to init it globally

例如全局初始化

    var cors = new EnableCorsAttribute("*", "*", "*");
    config.EnableCors(cors);

read more about it here... read section Scope Rules for [EnableCors]

在这里阅读更多相关内容...阅读部分[EnableCors]的范围规则

#2


1  

This is missing code on the server side, not in your browser AngularJS code. You do not need either of these lines with recent angularjs builds. These are both based on outdated information (They won't break anything, they are just unnecessary and based on outdated copypasta):

这是服务器端缺少的代码,而不是浏览器AngularJS代码。最近的angularjs版本不需要这些行中的任何一行。这些都是基于过时的信息(它们不会破坏任何东西,它们只是不必要的并且基于过时的copypasta):

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

Your server code needs to add the necessary CORS headers to its HTTP responses, so ask the maintainer to do so or post server side snippets and we can help with that.

您的服务器代码需要在其HTTP响应中添加必要的CORS标头,因此请维护人员这样做或发布服务器端代码片段,我们可以提供帮助。

#1


1  

you need to enable cors on server

你需要在服务器上启用cors

e.g. to init it globally

例如全局初始化

    var cors = new EnableCorsAttribute("*", "*", "*");
    config.EnableCors(cors);

read more about it here... read section Scope Rules for [EnableCors]

在这里阅读更多相关内容...阅读部分[EnableCors]的范围规则

#2


1  

This is missing code on the server side, not in your browser AngularJS code. You do not need either of these lines with recent angularjs builds. These are both based on outdated information (They won't break anything, they are just unnecessary and based on outdated copypasta):

这是服务器端缺少的代码,而不是浏览器AngularJS代码。最近的angularjs版本不需要这些行中的任何一行。这些都是基于过时的信息(它们不会破坏任何东西,它们只是不必要的并且基于过时的copypasta):

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

Your server code needs to add the necessary CORS headers to its HTTP responses, so ask the maintainer to do so or post server side snippets and we can help with that.

您的服务器代码需要在其HTTP响应中添加必要的CORS标头,因此请维护人员这样做或发布服务器端代码片段,我们可以提供帮助。