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

前端 未结 12 1437
有刺的猬
有刺的猬 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.

提交回复
热议问题