没想到多年之后我还得继续写这些东西.... 瀑布汗~

最近不得不开始研究用web api

MVC的项目中,在js文件里,实现点击一个按钮调用外部发布好的api,再从api把值回传给js页面,跳转。

经测试下面两种方法均有效获得api返回值。

function testinfo(id) {
$.ajax({
url: "http://158.14.51.103/api/Verify/Get",
type: 'GET',
dataType: 'JSON',
data: { Id: id, Key: "", FUrl: "./SalesSetting" },
success: function (data) {
alert("123 " + data.FUrl);
console.log(data.FUrl); //window.location.href = furl+id;
}
});
$.get('http://158.14.51.103/api/Verify/Get',
{ Id: id,Key:"",FUrl:"./SalesSetting"},
function (result) {
console.log(result.Key);
});
}

不过,最开始调用api的时候不停的报下面的错误。

XMLHttpRequest cannot load http://158.14.51.103/api/Verify/Get?Id=30054&Key=123456&Token=&FUrl=.%2FSalesSetting. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5955' is therefore not allowed access. The response had HTTP status code 404.

查了一下网上好多人说是chome浏览器的问题,但是在浏览器属性里加Access-Control-Allow-Origin字符串的方式并不好用,后来查到了下面这个网址,国外也有人在问这个问题。

http://stackoverflow.com/questions/27504256/mvc-web-api-no-access-control-allow-origin-header-is-present-on-the-requested

You need to enable CORS in your Web Api. The easier and preferred way to enable CORS globally is to add the following into web.config

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>

Please note that the Methods are all individually specified, instead of using *. This is because there is a bug occurring when using *.

You can also enable CORS by code.

把上面那段代码加入config就解决了~ 感动!

下面的网页也有提到这个问题

http://blog.csdn.net/starfd/article/details/45307659

2、config方式实现CORS

在Web.config的system.webServer配置节下增加配置,这种方式的好处是简单,只要在这里加了这个配置,那么所有的api都可以按同一种规则支持跨域请求

如果是HTTP Basic Access authentication,似乎还需要个<add name="Access-Control-Allow-Credentials" value="true" />,此处还未验证

最后补充个Cors相关说明:http://www.cnblogs.com/artech/p/cors-4-asp-net-web-api-02.html

以及官方链接:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

另外,关于web api 在IIS上的部署,我只想大声疾呼——IIS如果是后装的记得要注册记得要注册记得要注册!!!

好了,希望我能在web api的迫害下,顺利的活到下周!!!

=====================================================================================

好景不长。昨天好容易解决好了这个问题,今天居然故态重萌!!!

同样的报错出来了,检查webconfig,昨天加的一段代码还在,但是就是又报错说是跨域问题了。

头疼,搜了半天cors相关的,后来下面的文章救了我的命。

http://www.cnblogs.com/xhhha/p/3837277.html (百度上全是各种不要脸的网站转载,翻了半天才翻出来。。。这个大概应该是原作者吧~)

核心重点是这个部分:

3.Web API支持CORS

3.1打开VS,工具->库程序包管理器->程序包管理器控制台 ,输入下列命令:Install-Package Microsoft.AspNet.WebApi.Cors -Version 5.0.0  

注意 :目前Nuget 上面最新的版本是5.2.0 ,但是我测试,下载的时候,会有一些关联的类库不是最新的,System.Net.Http.Formatting 要求是5.2,我在网上找不带这dll,因此建议安装 :Microsoft.AspNet.WebApi.Cors 5.0就OK了。

Nuget 科普link:    http://www.cnblogs.com/dubing/p/3630434.html

因为在某次修改了api重新编译的时候,莫名其妙的弹出了这个对话框

然后程序会变得无比的慢,vs进入假死状态。这个提示到目前为止我都不明白什么意思,还原文件的名字是一直在动态变化,就是速度缓慢。

大概就是这个什么还原破坏了程序的哪个部分吧。。。。头痛,不想研究了。。。

最新文章

  1. MySQL主从复制中断,报“Error on master: message (format)=&#39;Cannot delete or update a parent row: a foreign key constraint fails&#39; error code=1217” 错误
  2. css3元素简单的闪烁效果(html5 jquery)
  3. 盒子模型简单理解(box-sizing)
  4. gulp 外挂 rename 的使用
  5. Best Coder Round#25 1001 依赖检测
  6. ArcGIS Flex API加载大量数据
  7. 剑指Offer:面试题16——反转链表(java实现)
  8. css自定义字体完美解决方案example
  9. 通过DDOS攻击流程图来浅谈如何预防Ddos攻击与防御
  10. Jquery 弹出提示框输入插件 apprise 修改中文按钮以及使用说明
  11. Inaccurate values for &ldquo;Currently allocated space&rdquo; and &ldquo;Available free space&rdquo; in the Shrink File dialog for TEMPDB only
  12. 转:ASP.NET中的SESSION实现与操作方法
  13. qq视频api代码
  14. C++重载赋值运算符
  15. MySQL到MongoDB的数据同步方法!
  16. IDEA2019激活码集合(非盈利)
  17. Hadoop-HA机制工作原理
  18. springboot加ES实现全局检索
  19. nginx使用ssl模块配置支持HTTPS访问,腾讯云申请免费证书
  20. 为了确认是您本人在申请搬家,请在原博客发表一 篇标题为《将博客搬至CSDN》的文章,并将文章地址填写在上方的&quot;搬家通知地址&quot;中

热门文章

  1. 用git管理自己读的书
  2. Nexus3.0.0+Maven的使用(一)
  3. 【解决】org.apache.hadoop.util.Shell$ExitCodeException: /bin/bash: line 0: fg: no job control
  4. [delphi]indy idhttp post方法
  5. APP开发和web开发的区别
  6. java单列设计模式 小记
  7. Hibernate之即时更新
  8. install cx_Oracle on Linux
  9. 简单的java socket 示例
  10. vi/vim 的使用