您的位置:首页 > Web前端 > JavaScript

在Angular.js中的H5页面调用Web api时跨域问题处理

2017-08-01 15:49 316 查看
/// <summary>

/// 被请求时

/// 在Angular.js中的H5页面调用Web api时跨域问题处理

/// </summary>

/// <param name="sender">请求者</param>

/// <param name="e">参数</param>

protected void Application_BeginRequest(object sender, EventArgs e)

{

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

if (HttpContext.Current.Request.HttpMethod == "OPTIONS")

{

HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "AccessCode, content-type");

HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");

HttpContext.Current.Response.End();

}

}

 

注:一般在Global.asax文件中增加此代码即可!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: