惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - fenix

IISExpress 开放局域网访问 sql窗口函数 Sql分页语句 常用SQL 数据库导出结构的sql 单进程运行 MahApps.Metro样式 iis7 添加Mime sudoku Geometric paths,mini language Accessing WMF metadata with C# 标点符号 摘抄 temp C# deep copy 数独 简单对称加密 ICommand ASP.NET 4 Child Applications Fail to Start When Under ASP.NET 2.0 or ASP.NET 3.5 Applications
WebAPI post 跨域调用及坑
fenix · 2016-01-13 · via 博客园 - fenix

nuget中安装Microsoft ASP.NET Web API Cors相关的两个包
EnableCors可以在方法 类上用EnableCorsAttibute设置
也可以在WebApiConfig的Register方法中,直接config.EnableCors()

IIS的设置,在web.config中
<system.webServer>中

<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS"/>
</customHeaders>
</httpProtocol>

WebAPI方法第一个参数使用[FromBody],post多个值,需要定义成一个对象,当成一个[FromBody]参数

1.

$(document).ready(function () {
var ecParams = {
"pID": 0,
"eItemIDs": ["01", "42"],
"startTime": "2016/01/10",
"endTime": "2016/01/11"
};
var prm = JSON.stringify(ecParams);
$.ajax(
{
//url: "http://xxx.xxx.xxx.xxx/xxxAPI/api/EItemDatas/getEItemData",
url: "../api/EItemDatas/getEItemData",
type: "post",
dataType: "json",
contentType: "application/json", 
data: prm
success: function (data) {
alert(data);
},
error: function (XHR, text, err) {
alert(text);
}
}
);
});

2.

$(document).ready(function () {
var ecParams = {
"pID": 0,
"eItemIDs": ["01", "42"],
"startTime": "2016/01/10",
"endTime": "2016/01/11"
};
//var prm = JSON.stringify(ecParams);
$.ajax(
{
url: "http://xxx.xxx.xxx.xxx/xxxAPI/api/EItemDatas/getEItemData",
//url: "../api/EItemDatas/getEItemData",
type: "post",
dataType: "json",
//contentType: "application/json", 
data: ecParams,
success: function (data) {
alert(data);
},
error: function (XHR, text, err) {
alert(text);
}
}
);
});


如上两段代码,第1段可以正常使用
第2段也可以正常使用,但是如果在第1段中调用跨域的接口会出现error,原因暂未知
ecParams的各个key跟getEItemData方法的参数类属性名完全一致