我知道这是一个经常发生的话题。我已经搜索了Internet,但是找不到解决我问题的方法:
我正在SAP Web IDE中开发SAPUI5 Fiori应用程序。该应用程序将部署在我们公司网络内的内部服务器上。因此,该应用只能在公司网络内运行。
现在我有了简单的ASP.NET Core Web API,该API在公司网络内部的单独服务器上运行。
[当我部署应用程序并尝试通过AJAX请求使用Web API时,出现以下错误:
从[http://myserver:5000/api/test访问XMLHttpRequest原点“ https://sap.mycompany.com:44315”已被CORS阻止策略:对预检请求的响应未通过访问控制检查:标题上没有'Access-Control-Allow-Origin'标头要求的资源。
我在Web API上的代码:
Startup.cs:
public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy( "AllowOrigin", builder => builder.WithOrigins("https://sap.mycompany.com:44315") .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials() ); }); services.AddAuthentication(IISDefaults.AuthenticationScheme); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.Configure<MvcOptions>(options => options.Filters.Add(new CorsAuthorizationFilterFactory("AllowOrigin"))); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseCors("AllowOrigin"); app.UseMvc(); }
控制器:
// POST: api/test [HttpPost] public async Task<IActionResult> test([FromBody] item) { try { // Do some stuff } catch (Exception ex) { // throw exception return BadRequest("Error"); } return Ok("Success"); }
我在Fiori前端上的代码:
控制器:
onTestButtonPress: async function (e) { $.ajax({ type: "POST", url: "http://myserver:5000/api/test", dataType: "json", crossDomain: true, data: $.param({ "Firstname":"John", "Lastname":"Rambo" }), success: function (response) { console.log("Success"); }, error: function (response) { console.log("Error"); } }); }
我尝试了任何操作,但在Google Chrome浏览器中仍然收到CORS错误。我还通过可用的网络安全性启动了Google Chrome浏览器:
chrome.exe --disable-web-security --disable-gpu
有人知道我在这里缺少什么吗?
非常感谢
我知道这是一个经常发生的话题。我已经搜索了Internet,但仍未找到解决问题的方法:我正在SAP Web IDE中开发SAPUI5 Fiori应用程序。该应用程序将是...
如果您的SAPUI5应用程序在SAP Cloud Platform上运行,则应将REST API定义为目标,然后从应用程序中使用该目标。