1、Global.asax中添加对Session的支持,重新Init方法:

public override void Init()
{
this.PostAuthenticateRequest += (sender, e)
=> HttpContext.Current.SetSessionStateBehavior
(System.Web.SessionState.SessionStateBehavior.Required);
base.Init();
}

2、WebConfig中添加跨域支持:

<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="http://*.*.*.*:8088" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>

3、写Session的Controller

[RoutePrefix("Reg")]
public class RegController : ApiController
{
[Route("RegUser")]
[HttpGet]
public bool RegUser(string userName)
{
System.Web.HttpContext.Current.Session["User"] = userName;
return true;
}
}

4、读Session的Controller

[RoutePrefix("Login")]
public class LoginController : ApiController
{
[Route("GetLogin")]
[HttpGet]
public string GetLogin()
{
string userName = string.Empty; if(System.Web.HttpContext.Current.Session["User"]!=null)
{
userName = System.Web.HttpContext.Current.Session["User"].ToString();
} return userName;
}
}

5、前端jQuery调用时加上参数crossDomain: true 和 xhrFields: { withCredentials: true}。


<script>

        var url = "http://*.*.*.*:8089/";
var userId = "002";
var userName = "xiaoming";
var password = "123456"; $(function () { btnRegClick();
btnGetNameClick();
}); function btnRegClick(){
$("#btnReg").click(function () { $.ajax({
type: "get",
url: url + "Reg/RegUser",
xhrFields: {
withCredentials: true
},
crossDomain: true,
data: {
userId: userId,
userName: userName,
password: password
},
success: function (result) {
$("#txtUserName").val(result)
}
}); });
} function btnGetNameClick(){
$("#btnGetName").click(function () { $.ajax({
type: "get",
url: url + "Login/GetLogin",
xhrFields: {
withCredentials: true
},
crossDomain: true,
data: {
},
success: function (result) {
$("#txtUserName").val(result)
}
}); });
} <script>

6、搞定。

WebAPI+Html跨域时对session的支持的更多相关文章

  1. web跨域访问,session丢失的问题

    web跨域访问,session丢失的问题25 http://www.iteye.com/problems/71265 http://www.iteye.com/topic/264079 具体情况如下: ...

  2. asp.net core webapi之跨域(Cors)访问

    这里说的跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被当作 ...

  3. 简析ASP.NET WebApi的跨域签名

    之前的文章写了关于WebApi的跨域问题,当中的方法只是解决了简单请求的跨域问题而非简单请求的跨域问题则没有解决. 要弄清楚 CORS规范将哪些类型的跨域资源请求划分为简单请求的范畴,需要额外了解几个 ...

  4. 关于WebService、WebApi的跨域问题

    随着移动互联网的发展, 传统营销模式往网站以及移动客户端转移已经成为一种趋势.接触过互联网开发的开发者肯定会很熟悉两种网络服务WebApi.WebService.在使用JavaScript进行数据交互 ...

  5. .NET压缩图片保存 .NET CORE WebApi Post跨域提交 C# Debug和release判断用法 tofixed方法 四舍五入 (function($){})(jQuery); 使用VUE+iView+.Net Core上传图片

    .NET压缩图片保存   需求: 需要将用户后买的图片批量下载打包压缩,并且分不同的文件夹(因:购买了多个用户的图片情况) 文章中用到了一个第三方的类库,Nuget下载 SharpZipLib 目前用 ...

  6. 在IE浏览器中iframe跨域访问cookie/session丢失的解决办法

    单点登录需要在需要进入的子系统B中添加一个类,用于接收A系统传过来的参数: @Action(value = "outerLogin", results = { @Result(na ...

  7. WebApi Ajax 跨域请求解决方法(CORS实现)

    概述 ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题, 特别 ...

  8. Http跨域时的Option请求

    写这篇文章时,我们碰到的一个场景是:要给R系统做一个扩展小应用,前端的html.js放在R系统里,后端需要做一个单独的站点N.B.com.这就导致了跨域问题,而且要命的是,后端同学没有权限向招聘的系统 ...

  9. WebApi Ajax 跨域请求解决方法(CORS实现)(作者:jianxuanbing)

    概述 ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题,特别各 ...

随机推荐

  1. Python float() 函数

    Python float() 函数  Python 内置函数 描述 float() 函数用于将整数和字符串转换成浮点数. 语法 float()方法语法: class float([x]) 参数 x - ...

  2. VMware安装Windows注意

    安装Windows通用步骤,分区,重建分区表,重写MBR引导.安装即可. VMware安装Windows 如果进不了CM/ROM,在.vmx文件里: 加入一行:bios.forceSetupOnce ...

  3. 关于mybatis缓存配置详解

    一级缓存: 一级缓存是默认的. 测试:在WEB页面同一个查询执行两次从日志里面看同样的sql查询执行两次. 2次sql查询,看似我们使用了同一个sqlSession,但是实际上因为我们的dao继承了S ...

  4. Codeforces 612B. Wet Shark and Bishops 模拟

    B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  5. Monte carlo

    转载 http://blog.sciencenet.cn/blog-324394-292355.html 蒙特卡罗(Monte Carlo)方法,也称为计算机随机模拟方法,是一种基于"随机数 ...

  6. 使用dos 作为中介实现cpython 和c# 交互

    最近在使用python 处理一些图形的东西. 实现:对一些512 的图进行像素遍历RGBA 变量, 查询通道不是 255 255 255 颜色 的矩阵,进行切图到空白 之前使用c#进行 确实快10 倍 ...

  7. arduino 中通过寄存器地址访问寄存器内容

    void call_func( void (*func)(void)){ (*func)(); } void setup() { // put your setup code here, to run ...

  8. 怎么备份VMware虚拟磁盘文件或移植到其他虚拟机

    原文:http://jingyan.baidu.com/article/a681b0de17b3173b1843468f.html 方法/步骤     第一种方法:直接复制本地主机磁盘下的虚拟磁盘文件 ...

  9. UESTC 486 Good Morning (水题+坑!)

    题意:给你一行字符串,让你找其中蕴含的“good morning"的次数. 析:看起来很水么,多简单,只有统计一下其中字母的出现的次数,然后除以相应的个数. 但是很不幸的是WA,而且是在te ...

  10. AngularJS标准Web业务流程开发框架-4.AngularJS四大模块之一:Controller

    一.Controller的创建 angular.controller("name",funtion($scope){ }) 1.name:控制器的名称(建议参考Java包的命名规范 ...