asp.net core webapi 直接浏览器访问web api访问成功,但是用js跨域访问就访问不到了?

brain707 2019-05-28 05:44:04
Controller代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace WebApplication2.Controllers
{
[Route("api/[controller]")]
[EnableCors("any")]
[ApiController]
public class TestController : Controller
{

// GET api/test/5
[HttpGet("{id}")]
[EnableCors("any")]
public string Get(int id)
{
HttpContext.Session.SetString("name", "中文");
string result = HttpContext.Session.GetString("name");
return result.ToString();
}

// GET api/test
[HttpGet]
public string Get()
{
try
{
string result = HttpContext.Session.GetString("name");
return "name:"+result.ToString();
}
catch (Exception ex)
{
return ex.ToString();
}
}

}
}

Startup代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace WebApplication2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//Session 保存到内存
services.AddDistributedMemoryCache();
services.AddSession();

//配置跨域处理
services.AddCors(options =>
{
options.AddPolicy("any", builder =>
{
builder.AllowAnyOrigin() //允许任何来源的主机访问
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();//指定处理cookie
});
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

}



// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
...全文
1067 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2019-05-28
  • 打赏
  • 举报
回复
#2正解。 首先要后台增加对于提交请求的支持。 然后ajax需要携带认证信息
  • 打赏
  • 举报
回复
brain707 2019-05-28
  • 打赏
  • 举报
回复
js代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="jquery.js"></script> <script> $.ajax({ type: 'GET', url: 'http://10.10.1.112:8000/api/test/5', xhrFields: { withCredentials:true //配置http跨域请求中携带cookie }, success: function (data) { alert(data); }, error: function () { alert('登录发生错误!'); } }); </script> </head> <body> </body> </html>

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧