zoukankan      html  css  js  c++  java
  • 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) { ****** } }
  • 相关阅读:
    数组是个好东西
    排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。
    子序列的和
    韩信点兵
    水仙花数
    阶乘之和
    3n+1问题
    MongoDB 安装
    mysql中bigint在php中表示
    Android之NDK开发
  • 原文地址:https://www.cnblogs.com/wxl-handsome-man/p/9518756.html
Copyright © 2011-2022 走看看