zoukankan      html  css  js  c++  java
  • ajax跨域通过 Cors跨域资源共享 进行GetPost请求

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web.Http;
    using System.Web.Http.Controllers;
    
    namespace Common
    {
        public class Cors : DelegatingHandler
        {
    
            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                HttpResponseMessage response = base.SendAsync(request, cancellationToken).Result;
                response.Headers.Add("Access-Control-Allow-Origin", "*");
                return Task.FromResult<HttpResponseMessage>(response);
            }
        }
    }

    首先重写  DelegatingHandler 中的 SendAsync 方法

    就这么三行,其目的是给响应头中添加  Access-Control-Allow-Origin:* 这一句话

    然后再Global文件中注册这个方法

    GlobalConfiguration.Configuration.MessageHandlers.Add(new CorsHandler());

    然后这个时候就可以直接用Ajax方法调用我们项目中的各种方法了。

    Ajax方法的url 参数一定要写绝对路径。

  • 相关阅读:
    负外边距--转载
    研究Dropbox Server端文件系统
    Bluetooth Profile for iPhone from the functional perspectives
    Somebody That I Used to Know
    复合查询
    聚合查询
    Filter查询
    ES基本查询
    ES版本控制
    ES基本操作
  • 原文地址:https://www.cnblogs.com/ansheng/p/5718353.html
Copyright © 2011-2022 走看看