.net MVC下跨域Ajax请求(CORS)
2024-09-30 04:12:49
二、CROS (Cross-origin Resource Sharing)
CROS相当于一种协议,由浏览器、服务端共同完成安全验证,进行安全的跨域资源共享。对于开发人员来说就跟在本站AJAX请求一样,浏览器会自动判断是否使用CROS。
客户端:
<script type="text/javascript">
function TestCors() {
$.ajax({
type: "GET",
url: "http://hamp-sz-0104/MVC/Books/Test",
dataType: "json",
success: function (obj) {
alert(obj.Name);
}
})
}
</script>
服务端:
public class BooksController : Controller
{
public ActionResult Test()
{
return Json(new { Name = "CORS" },JsonRequestBehavior.AllowGet);
}
}
服务端配置:需要给允许CORS请求的消息设置Access-Control-Allow-Origin header值,或使用“*”授权任何域名可访问
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:3794"/>
</customHeaders>
</httpProtocol>
</system.webServer></configuration>
若无配置,浏览器会收到如下错误提示:SEC7120: [CORS] 原点“http://localhost:3794”未在“http://hamp-sz-0104/MVC/Books/JsonpTest”的 cross-origin 资源的 Access-Control-Allow-Origin response header 中找到“http://localhost:3794”。
最新文章
- GO语言练习:多返回值函数
- Repeater分页
- CUDA 程序中的同步
- jboolean
- [原博客] POJ 1067 取石子游戏
- void (*fun)(void);什么意思?
- webkit内核下的mouseup后mousemove自动触发问题及解决方法
- 在开源中国(oschina)上创建托管项目
- Struts2(六) 用Struts完成客户列表显示
- win10只有edge浏览器能上网的解决方法
- notes for lxf(三)
- Codeup
- MySQL--运维内参中的binlog_summary脚本
- jdbc的配置(更新中)
- Java 如何实现在线预览文档及修改(文本文件)
- Postman—使用数据文件
- (转)web性能优化
- 【原创】MySQL常用脚本整理
- MySQL 关于索引那点事
- ELK Stack 5.2.2 安装文档
热门文章
- 436. Find Right Interval
- [Offer收割]编程练习赛46
- 骨骼蒙皮动画(SkinnedMesh Animation)的实现
- Inside Geometry Instancing(上)
- spoj3105 MOD - Power Modulo Inverted(exbsgs)
- Java 虚拟机(Java Virtual Machine)
- bzoj4563 HAOI2016放旗子
- Sublime text3 插件ColorPicker(调色板)不能使用快捷键
- IP服务-5-网络时间协议
- 问题: 查看某个文件的修改记录| git log 高级用法