• c# 跨域api


    前端 ajax get请求

     $.ajax({
                url: "API地址",
                type: 'get',
                dataType: 'jsonp',
                async: true,
                processData: true,
                contentType: false,
    
                success: function (res) {
                    console.log(res)
                },
                error: function (Error) {
                    console.log(Error)
                }
            })

    前端 ajax post请求

    $.ajax({
                type: 'post',
                url: 'API地址',
                data: data,
                dataType: "json",
                crossDomain: true,
                success: function (data) {
                    console.log(data);
    
                },
                error: function (data) {
                    console.log(data);
    
                }
            });

    c# 后端 get请求

    var res="最好转换成json";
    string callback = this.Request["callback"];
    return Content(callback + "(" + res + ")");

    c# 后端 post 请求

    //1.可以创建一个FilterAttribute 
    public class PostAPIController : ActionFilterAttribute
        {
         //override  OnActionExecuting
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                //设置
                filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Methods", "POST");
                filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Headers", "x-requested-with,content-type");
    
                base.OnActionExecuting(filterContext);
            }
        }



        //2.post请求代码块   加上过滤器 PostAPIController
        
            [HttpPost]
            [PostAPIController]    
            public ActionResult AddReadNum(int newid)
            {
                try
                {
                    *******
              *******
    if (string.IsNullOrEmpty(callback)) { return Json(result, JsonRequestBehavior.AllowGet); } else { string res = JsonToots.ModelToJson(result); return Content(callback + "(" + res + ")"); } } catch (Exception ex) { ****** } }
  • 相关阅读:
    hdu 6201 dfs
    Oulipo POJ
    Kitchen Measurements UVALive
    Surf Gym
    hoj 13969 Racing Gems
    分块
    分块学习资料
    Jam's problem again HDU
    树的点分治
    Census UVA
  • 原文地址:https://www.cnblogs.com/wxl-handsome-man/p/9518756.html
Copyright © 2020-2023  润新知