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) { ****** } }
  • 相关阅读:
    浅谈三层模式
    javascript的全局变量
    BZOJ 3668 NOI2014 起床困难综合症 贪心
    调试经验--图像
    Mac OS X 10.10 执行 Eclipse 提示须要安装 Java
    ubuntuOS
    BLOB存储图片文件二进制数据是非对错
    API经济产业
    python模块目录文件后续
    MongoDB命令
  • 原文地址:https://www.cnblogs.com/wxl-handsome-man/p/9518756.html
Copyright © 2011-2022 走看看