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,注意这里

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

posted @ 2021-06-01 16:48  古兴越  阅读(479)  评论(0)    收藏  举报