首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息:

查看控制台会发现错误:XMLHttpRequest cannot load http://localhost:81/api/. No ‘Access-Control-Allow-Origin‘ header is present on the requested resource. Origin ‘http://localhost:53784‘ is therefore not allowed access.

这时会发现并没有阻止请求的发送,而是阻止了不同源请求的响应。也就是说请求没有问题,问题出在了响应。那么如何对指定的源开放访问???

第一步:首先在工具里面打开输入;Install-Package Microsoft.AspNet.WebApi.Cors  回车。

第二步:等第一步完成后 打开App_Start/WebApiConfig.cs. 添加一下代码:  config.EnableCors();

第三步:在需要访问的Controlls上面加这样的属性:  [EnableCors(origins: "http://localhost:53784", headers: "*", methods: "*")]

现在可以再试试看看效果。如果可以请给个赞 希望帮助到更多的人。

Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题的更多相关文章

  1. As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource

    默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:228 ...

  2. 解决跨域No 'Access-Control-Allow-Origin' header is present on the requested resource.

    用angular发起http.get(),访问后端web API要数据,结果chrome报错:跨域了 Access to XMLHttpRequest at 'http://127.0.0.1:300 ...

  3. java、ajax 跨域请求解决方案('Access-Control-Allow-Origin' header is present on the requested resource. Origin '请求源' is therefore not allowed access.)

      1.情景展示 ajax调取java服务器请求报错 报错信息如下: 'Access-Control-Allow-Origin' header is present on the requested ...

  4. .Net Core 处理跨域问题Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

    网页请求报错: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Or ...

  5. 解决Tomcat错误信息:No 'Access-Control-Allow-Origin' header is present on the requested resource | Solving Tomcat Error: No 'Access-Control-Allow-Origin' header is present on the requested resource

    最近在使用GeoServer调用Vector Tile服务时,经常会显示不出来结果.打开浏览器调试台,发现报No 'Access-Control-Allow-Origin' header is pre ...

  6. 跨域问题解决----NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost:11000' is therfore not allowed access'

    NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost ...

  7. WCF REST开启Cors 解决 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.

    现象: 编写了REST接口: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemp ...

  8. js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource

    js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...

  9. (转)AJax跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource

    在本地用ajax跨域访问请求时报错: No 'Access-Control-Allow-Origin' header is present on the requested resource. Ori ...

随机推荐

  1. SQL Server进阶 遍历表的几种方法

    https://www.cnblogs.com/mcgrady/p/4182486.html

  2. Groovy 设计模式 -- Strategy 模式

    策略模式 https://en.wikipedia.org/wiki/Strategy_pattern In computer programming, the strategy pattern (a ...

  3. JS算法练习二

    JS算法练习 1.生成4位的随机验证码,可取大小写字母和数字 ? var validateCode = "", /*--存放生成好的验证码字符串--*/ count = 0; /* ...

  4. Java SE之正则表达式二:匹配

    package demo.regex; import java.util.regex.Pattern; /* 正则表达式:匹配 */ public class RegexMatchesDemo { / ...

  5. luogu P4173 残缺的字符串

    传送门 两种做法,一种是依次考虑每种字符,然后如果某个位置是该字符或者是\(*\)对应的值就是1,否则是0,然后把第一个串倒过来,fft卷积起来,最后看对应位置的值是否为m 然而上面那个做法在字符集大 ...

  6. 导弹拦截 dp

    n∗lognn*lognn∗logn写法,lis[i]的意义为:所有最长上升子序列长度为i的位置上的最小a数组元素值lis[i]的意义为:所有最长上升子序列长度为i的位置上的最小a数组元素值lis[i ...

  7. centos7 安装.net core的方法

    安装: sudo yum install libunwind libicu curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?li ...

  8. ubuntu 简单安装配置gitlab

    安装 gitlab-ce 社区版 依赖 sudo apt-get install curl openssh-server ca-certificates postfix 添加gitlab包服务并安装 ...

  9. D. Time to go back(思维)

    题目链接:http://codeforces.com/gym/100952/problem/D 题目大意:n个礼物,m个人,要给m个人中的k个人买大于等于d的礼物,其他人随意,问你选择礼物的方案数(不 ...

  10. linux 文件处理命令