Lotus@ 2019-11-25 12:54 采纳率: 100%
浏览 105

SAPUI5-AJAX CORS问题

I know this is a topic that occures a lot. I have searched the Internet but I could not find a solution for my problem yet:

I am developing an SAPUI5 Fiori app in the SAP Web IDE. The App will be deployed on an internal server within our company network. So the app will only work inside the company network.

Now I have simple ASP.NET Core Web API that is running on a separate server also inside the company network.

When i deploy the app and try to consume the web api via AJAX request, i become the following error:

Access to XMLHttpRequest at 'http://myserver:5000/api/test' from origin 'https://sap.mycompany.com:44315' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

My Code on the 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();

}

Controller:

// 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");         
}

My Code on the Fiori-Frontend:

Controller:

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");
        }
    });
}

I Have tried anything but I still receive the CORS error in Google Chrome. I also startet Google Chrome with diabled web-security:

chrome.exe --disable-web-security --disable-gpu

Does anyone have any idea what I am missing here?

Thanks a lot

  • 写回答

3条回答 默认 最新

  • weixin_33688840 2019-11-25 13:57
    关注

    If your SAPUI5 app runs on SAP Cloud Platform, you should define your REST API as a Destination and then consume that destination from your app.

    If it runs on SAP Gateway (or any other web server you have control of), you may configure your server to respond with header like ‘Access-Control-Allow-Origin’ : ‘http://myserver:5000/api/test’

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调