No 'Access-Control-Allow-Origin' header is present on the requested resource- AngularJS

前端 未结 12 1045
有刺的猬
有刺的猬 2020-11-27 03:18
XMLHttpRequest cannot load http://mywebservice. No \'Access-Control-Allow-Origin\' header is present on the requested resource. Origin \'http://localhost:9000\' is t         


        
相关标签:
12条回答
  • 2020-11-27 04:12

    I have a solution below and its works for me:

    app.controller('LoginController', ['$http', '$scope', function ($scope, $http) {
    $scope.login = function (credentials) {
    $http({
            method: 'jsonp',
            url: 'http://mywebservice',
            params: {
                format: 'jsonp',
                callback: 'JSON_CALLBACK'
            }
        }).then(function (response) {
            alert(response.data);
        });
      }
    }]);
    

    in 'http://mywebservice' there must be need a callback parameter which return JSON_CALLBACK with data.
    There is a sample example below which works perfect

    $scope.url = "https://angularjs.org/greet.php";
        $http({
            method: 'jsonp',
            url: $scope.url,
            params: {
                format: 'jsonp',
                name: 'Super Hero',
                callback: 'JSON_CALLBACK'
            }
        }).then(function (response) {
            alert(response.data);
        });
    

    example output:

    {"name":"Super Hero","salutation":"Apa khabar","greeting":"Apa khabar Super Hero!"}
    
    0 讨论(0)
  • 2020-11-27 04:16

    I got

    No 'Access-Control-Allow-Origin' header is present

    and the problem was with the URL I was providing. I was providing the URL without a route, e.g., https://misty-valley-1234.herokuapp.com/.

    When I added a path it worked, e.g., https://misty-valley-1234.herokuapp.com/messages. With GET requests it worked either way but with POST responses it only worked with the added path.

    0 讨论(0)
  • 2020-11-27 04:17

    On the client side you can enable cors requests in AngularJS via

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

    However if this still returns an error, this would imply that the server that you are making the request has to allow CORS request and has to be configured for that.

    0 讨论(0)
  • 2020-11-27 04:17

    If you are using chrome: try open chrome with the args to disable web security like you see here:

    Disable same origin policy in Chrome

    0 讨论(0)
  • 2020-11-27 04:18

    The Chrome Webstore has an extension that adds the 'Access-Control-Allow-Origin' header for you when there is an asynchronous call in the page that tries to access a different host than yours.

    The name of the extension is: "Allow-Control-Allow-Origin: *" and this is the link: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

    0 讨论(0)
  • 2020-11-27 04:21

    I added this and it worked fine for me.

    web.api

    config.EnableCors();
    

    Then you will call the model using cors:

    In a controller you will add at the top for global scope or on each class. It's up to you.

     [EnableCorsAttribute("http://localhost:51003/", "*", "*")]
    

    Also, when your pushing this data to Angular it wants to see the .cshtml file being called as well, or it will push the data but not populate your view.

    (function () {
        "use strict";
        angular.module('common.services',
            ['ngResource'])
        .constant('appSettings',
        {
            serverPath: "http://localhost:51003/About"
        });
    }());
    
    //Replace URL with the appropriate path from production server.
    

    I hope this helps anyone out, it took me a while to understand Entity Framework, and why CORS is so useful.

    0 讨论(0)
提交回复
热议问题