• C# 跨域


    后端测试方法

    namespace WebApplication4.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
    
            [AllowCrossSiteJson]
            public JsonResult add(string name)
            {
                return Json(new { id = 1, name = name });
            }
    
    
            [HttpPost, AllowCrossSiteJson]
            public JsonResult add2(string name)
            {
                return Json(new { id = 1, name = name });
            }
    
            [HttpPost, AllowCrossSiteJson]
            public JsonResult add3(model1 model)
            {
                return Json(model);
            }
    
    
    
            [HttpGet, AllowCrossSiteJson]
            public ContentResult get1(string name)
            {
                return Content(JsonConvert.SerializeObject(new { id = 1, name = name }));
            }
    
            [HttpGet, AllowCrossSiteJson]
            public ContentResult get2(string name)
            {
                return Content(JsonConvert.SerializeObject(new { id = 1, name = name }), "application/json; charset=utf-8");
            }
    
        }
    
    
        public class model1
        {
            public string name { get; set; }
        }
    
    }
    

    前端测试方法

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
        <script type="text/javascript">
    
            $(function () {
                //$.post("http://localhost:50507/Home/add", { name: "name111" }, function (res) {
                //    console.log(res);
                //});
    
                //$.get("http://localhost:50507/Home/get1", { name: "name111" }, function (res) {
                //    console.log(res);
                //});
    
                //$.ajax({
                //    type: "get",
                //    url: "http://localhost:50507/Home/get1",
                //    data: { name: "name111" },
                //    dataType: 'json',
                //    xhrFields: {
                //        withCredentials: true
                //    },
                //    crossDomain: true,
                //    success: function (response) {
                //        console.log(response);//返回的内容
                //    }
                //});
    
    
    
    
    
                //$.get("http://localhost:58059/BimAppLication/BimApi/GetProjectByUser",function (res) {
                //    console.log(res);
                //});
    
    
    
                //$.get("http://localhost:58059/BimAppLication/BimApi/GetComponentInfoByCode", {code:"123"}, function (res) {
                //    console.log(res);
                //});
    
    
                //$.post("http://localhost:58059/BimAppLication/BimApi/SaveBimPostion", { MoldeName: "123" }, function (res) {
                //    console.log(res);
                //});
    
    
                //$.ajax({
                //    type: "POST",
                //    url: "http://localhost:58059/BimAppLication/BimApi/SaveBimPostion",
                //    data: { MoldeName: "123" },
                //    dataType: 'json',
                //    xhrFields: {
                //        withCredentials: true
                //    },
                //    crossDomain: true,
                //    success: function (response) {
                //        console.log(response);//返回的内容
                //    }
                //});
    
    
    
                $.post("http://localhost:50507/Home/add", { name: "name111" }, function (res) {
                    console.log(res);
                });
    
                $.post("http://localhost:50507/Home/add2", { name: "name111" }, function (res) {
                    console.log(res);
                });
    
                $.post("http://localhost:50507/Home/add3", { name: "name111" }, function (res) {
                    console.log(res);
                });
    
    
    
                $.ajax({
                    type: "get",
                    url: "http://localhost:58059/BimAppLication/BimApi/GetComponentInfoByCode",
                    data: { code: "123" },
                    dataType: 'json',
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    success: function (response) {
                        console.log(response);//返回的内容
                    }
                });
    
    
                $.get("http://localhost:50507/Home/get2", { name: "name111" }, function (res) {
                    console.log(res);
                });
    
    
    
            });
    
        </script>
    
    </body>
    </html>
    

    如果前端是vue,注意这里

    没有加,好像就是这种样子

  • 相关阅读:
    lua table的遍历
    代码动态更换MeshRenderer的材质Materials
    随手一记-单例模式写法
    unity UGUI填坑 之 HorizontalLayoutGroup 和 ContentSizeFitter配合使用
    unity 使用OnDrag实现物体旋转
    unity用json和服务器数据交互
    C语言核心技术-C语言概述与开发环境搭建
    Linux系统运维与架构设计-浅谈计算机系统
    Linux系统运维与架构设计-Unix/Linux概述
    Java从0到全栈-Java语言概述与开发环境搭建
  • 原文地址:https://www.cnblogs.com/guxingy/p/14837891.html
Copyright © 2020-2023  润新知