1

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

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.

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.

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);
7
  • can you show what you see in your network tab... response?? Commented Nov 27, 2014 at 16:30
  • @harish Appears This request has no response data available., with a Error 500 Commented Nov 27, 2014 at 16:32
  • You cannot simply enable cors by setting a header on the client side, the server you are making the request to has to have CORS enabled Commented Nov 27, 2014 at 16:32
  • can you share what have you tried on server side? how you enabled cors?? cors enabling is server side decision as well... Commented Nov 27, 2014 at 16:34
  • 1
    read here Commented Nov 27, 2014 at 17:01

2 Answers 2

1

you need to enable cors on server

e.g. to init it globally

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

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

Sign up to request clarification or add additional context in comments.

Comments

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):

$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.