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

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

提交回复
热议问题