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

前端 未结 12 1085
有刺的猬
有刺的猬 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 03:58

    This is a CORS issue. There are some settings you can change in angular - these are the ones I typically set in the Angular .config method (not all are related to CORS):

    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
    

    You also need to configure your webservice - the details of this will depend on the server side language you are using. If you use a network monitoring tool you will see it sends an OPTIONS request initially. Your server needs to respond appropriately to allow the CORS request.

    The reason it works in your brower is because it isn't make a cross-origin request - whereas your Angular code is.

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

    In my case, I was trying to hit a WebAPI service on localhost from inside an MVC app that used a lot of Angular code. My WebAPI service worked fine with Fiddler via http://localhost/myservice. Once I took a moment to configure the MVC app to use IIS instead of IIS Express (a part of Visual Studio), it worked fine, without adding any CORS-related configuration to either area.

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

    This is how it worked for me. For Windows users testing with Bracket and AngularJS

    1) Go to your desktop

    2) Right click on your desktop and look for "NEW" in the popup drop down dialog box and it will expand

    3) Choose Shortcut

    4) A dialog box will open

    5) Click on Browse and look for Google Chrome.

    6) Click Ok->Next->Finish and it will create the google shortcut on your desktop

    7) Now Right Click on the Google Chrome icon you just created

    8) Click properties

    9) Enter this in the target path

    "C:\Program Files\Google\Chrome\Application\chrome.exe" --args --disable-web-security
    

    10) Save it

    11) Double click on your newly created chrome shortcut and past your link in the address bar and it will work.

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

    Use this extension for chrome. Allows to you request any site with ajax from any source. Adds to response 'Allow-Control-Allow-Origin: *' header

    https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi/related

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

    Replace get with jsonp:

     $http.jsonp('http://mywebservice').success(function ( data ) {
        alert(data);
        });
    }
    
    0 讨论(0)
  • 2020-11-27 04:10

    It is a problem on the server side. You have to add your client address to your server exposed API. If you are using Spring frame work you can annotate @CrossOrgin from org.springframework.web.bind.annotation.CrossOrigin;

    Eg : @CrossOrigin(origins = "http://localhost:8080")

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