zoukankan      html  css  js  c++  java
  • 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,注意这里

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

  • 相关阅读:
    如何给远程主机开启mysql远程登录权限
    Session机制详解
    CentOS 下PHP的卸载
    PHP实现执行定时任务的几种思路详解
    容易产生错误的where条件
    php超时任务处理
    (转载)Android项目tab类型主界面总结
    使用xutils发送POST请求,携带json和图片二进制文件数据获取服务器端返回json数据
    Android开发中常见错误
    (转载)Android显示原理简介
  • 原文地址:https://www.cnblogs.com/guxingy/p/14837891.html
Copyright © 2011-2022 走看看