62,046
社区成员
发帖
与我相关
我的任务
分享
$(function () {
var model = {};
model.refUrl = "ssssssssss";
$.ajax({
async: false,
url: "http://localhost:5566/AccessRecord/StartPost",
type: "post",
data: model,
dataType: "json",
timeout: 10000,
success: function (json) {
alert(1);
//alert(JSON.stringify(json));
//debugger;
},
error: function (msg) {
//alert("error");
//alert(JSON.stringify(msg));
debugger;
}
});
})
public ActionResult StartPost(FloorDTO dto)
{
return Json(new RtnDTO() { result = "OK", msg = dto.refUrl }, JsonRequestBehavior.DenyGet);
}
<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>
.....
</system.webServer>
XMLHttpRequest cannot load http://localhost:5566/AccessRecord/StartPost. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:6602' is therefore not allowed access.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace System.Web.Mvc
{
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
}
这样比较好
[ AllowCrossSiteJson]
public ActionResult StartPost(FloorDTO dto)using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace System.Web.Mvc
{
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
}
这样比较好
[ AllowCrossSiteJson]
public ActionResult StartPost(FloorDTO dto)