net core api 后台处理跨域请求

网上这方面很大,别人怎么写我照着写,ok 记下来以后增加记忆 

 

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options => options.AddPolicy("DomainKYHttp",
          builder => builder.AllowAnyMethod()
          .AllowAnyHeader()
          .AllowAnyOrigin()
          .AllowCredentials()));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

 

 

 

 

 

 

         }
            app.UseCors("DomainKYHttp");
            app.UseHttpsRedirection();
            app.UseMvc();
        }

 

 

 

#region 跨域 指定特定的请求路径
            var urls = Configuration["AppConfig:Cores"].Split(',');
            services.AddCors(options =>
            options.AddPolicy("AllowSameDomain",
        builder => builder.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin().AllowCredentials())
            );
            #endregion

 

 
     


 [HttpGet("{id}/{name}")]
        public async Task<JsonResult> Get(int id, string name)
        {
            return new JsonResult(await Task.Run(()=>id+"  "+name));
        }

[HttpPost]
        public async Task<IActionResult> Post([FromBody]dynamic user)
        {
            return new ObjectResult(await Task.Run(() => user));
        }


     

  // PUT: api/GetMvc/5
        [HttpPut("{id}")]
        public async Task<JsonResult> Put(int id, [FromBody]string value)
        {
            return new JsonResult(await Task.Run(() => id + JsonConvert.SerializeObject(value)));
        }


        //[HttpPut("{id}")]
        //public JsonResult Put(int id, [FromBody] string value)
        //{
        //    return new JsonResult(value);
        //}

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public async Task Delete(int id)
        {
            await Task.Run(() => id);
        }

        //[HttpDelete("{id}")]
        //public void Delete(int id)
        //{
        //}

 

 

前端

 

  $.get("https://localhost:44352/api/GetMvc/145/ewfaf", function (data) {
            alert(data);
        });

        //
        //跨域
        post
        $.ajax({
            url: "https://localhost:44352/api/GetMvc",
            type: "POST",
            contentType: "application/json; charset=utf-8", //跨域
            data: '{ "UserID": 1, "UserName": "test", "UserEmail": "test@cnblogs.com" }',
            dataType: 'json',
            success: function (data) {
                debugger;
                alert(JSON.stringify(data));
            }
        });
        //put

        $.ajax({
            url: "https://localhost:44352/api/GetMvc/123",
            type: "put",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify("Jeffcky"),
            success: function (data) {
                alert(data);
            }
        });

        $.ajax({
            url: "https://localhost:44352/api/GetMvc/123",
            type: "Delete",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {}
            
        })

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值