首页 » 网站建设 » muialertphp技巧_C Controller下的Action对 Ajax跨域访问设备

muialertphp技巧_C Controller下的Action对 Ajax跨域访问设备

duote123 2024-12-13 0

扫一扫用手机浏览

文章目录 [+]

在节点<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> <!--许可跨域 结束-->

2)添加许可访问的基类

muialertphp技巧_C Controller下的Action对 Ajax跨域访问设备

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc; namespace LintwaySales{ /// <summary> /// 指定站点许可跨域访问(根本类) /// </summary> public class AllowOriginAttribute { public static void onExcute(ControllerContext context, string[] AllowSites) { var origin = context.HttpContext.Request.Headers["Origin"]; Action action = () => { context.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", origin); }; if (AllowSites != null && AllowSites.Any()) { if (AllowSites.Contains(origin)) { action(); } } } } public class ActionAllowOriginAttribute : ActionFilterAttribute { public string[] AllowSites { get; set; } public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext) { AllowOriginAttribute.onExcute(filterContext, AllowSites); base.OnActionExecuting(filterContext); } } public class ControllerAllowOriginAttribute : AuthorizeAttribute { public string[] AllowSites { get; set; } public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext) { AllowOriginAttribute.onExcute(filterContext, AllowSites); } }}

muialertphp技巧_C Controller下的Action对 Ajax跨域访问设备
(图片来自网络侵删)

3)修正Action方法

/// <summary> /// 根据批次主键获取批次 /// </summary> /// <param name="BatchID"></param> /// <returns></returns> /// 指定Action许可跨域访问 [HttpPost] [ActionAllowOrigin(AllowSites = new string[] { "" })] public ActionResult GetBatch(string BatchID) { string exceptionAction = MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name; //获取命名空间 + 类名 + 方法名 try { var data = _BatchServer.GetBatch(BatchID); return Json(new { code = MyEnum.ResponseStatus.Success, data = data, msg = "" }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { string Remark = ((int)MyEnum.LogType.ErrorLog).ToString(); _ExceptionLogServer.AddLog(ex.ToString(), exceptionAction, System.Guid.Empty, Remark); return Json(new { code = MyEnum.ResponseStatus.Fail, data = "", msg = ex.ToString() }, JsonRequestBehavior.AllowGet); } }

4)Jquery Ajax调用Action

$.ajax({ type: "POST", dataType: "json", url: base.ServerBaseUrl + 'Batch/GetBatch', data: { BatchID: BatchID }, success: function (result) { if (result.code == base.ResponseSuccess) { var data = result.data; } else { mui.alert(base.QRCodeError, base.alertMsg); } }, error: function () { mui.alert(base.GetDataFail, base.alertMsg); }, complete: function () { layer.close(index); }});

标签:

相关文章

phpmcryptblowfish技巧_简述Blowfish加密算法

Blowfish加密算法的特点:1. 对称加密,即加密的密钥和解密的密钥是相同的;2. 每次加密之后的结果是不同的(这也是老夫比较...

网站建设 2024-12-15 阅读0 评论0

学IT之路,探索数字时代的知识之旅

在数字化时代的浪潮中,信息技术(IT)已经渗透到社会生活的方方面面。对于有志于投身IT行业的人来说,学习IT之路既充满挑战,又充满...

网站建设 2024-12-15 阅读0 评论0