cancel
Showing results for 
Search instead for 
Did you mean: 

CORS Orgin problem in a CAP project

0 Kudos
2,284

Hello experts !

I have developed a CAP application with 2 frontends:

(1) - Fiori UI5 : Maintain videos database: CRUD

(2) - Classic HTML/CSS/JS : Displays videos

Now I have a CORS error in the second project.. where I cannot load the data fetched from JSON

In this screenshot you can see the errors that occurs while requesting data from the backend.

And in the line 82 of Checkbox.js : (Client side)

fetch("https://url.hana.ondemand.com/service/front/Topics")

.then((Response) => Response.json())

.then((topics) => { ..}

I read this blog CORS and Fiori/UI5 – Everything you need to know | SAP Blogs but it does not seem to be working with this project.Does anyone have faced this issue earlier or any solution to propose.

Thank you !

Best,

Moomen

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hello developers,

Depending on your case, I invite you to visit this page GitHub - SAP-samples/devtoberfest-2021-security-coding-challenge: The sample in this repository has ...

You can find all, or nearly all, of the security problems that you may face during the development of a SAP CAP model project.

Thanks.

Best,

Moomen

Answers (2)

Answers (2)

gregorw
SAP Mentor
SAP Mentor

You can avoid this issue when you use the approuter and a destination to the CAP backend. Then you don't have to specify a absolute URL. Instead you can use a relative path to the backend.

0 Kudos

Thank you gregorw for your support !

I am trying to create one but I am not sure that I am on the right way .. Do you have any idea, how to do that ? Maybe a blog or article may help ..

gregorw
SAP Mentor
SAP Mentor
0 Kudos
0 Kudos

Thanks gregorw !

Right ! there's a lot of right solutions to solve this problem.

In my case, I have created a file 'server.js' and simply added this code:

const cds = require('@sap/cds')cds.on('bootstrap', (app) => { const cors = require('cors') app.use(cors())})
module.exports = cds.serverand it's working just fine.
gregorw
SAP Mentor
SAP Mentor
0 Kudos

But how do you authenticate to the CAP Service when you call it that way?

yogananda
Product and Topic Expert
Product and Topic Expert

moomenmufti

you can install this

npm i cors

// example
var express = require('express')
var cors = require('cors')var app = express() app.options('/products/:id', cors()) // enable pre-flight request for DELETE requestapp.del('/products/:id', cors(), function (req, res, next) { res.json({msg: 'This is CORS-enabled for all origins!'})}) app.listen(80, function () { console.log('CORS-enabled web server listening on port 80')})
0 Kudos
Hello yoganandamuthaiah Thank you for your response !In fact, I am only trying to read/GET the data from the JSON and I've tried to create a JS file where I did this ( also I tried your way ):var express = require ('express');const app = express();var cors = require ('cors');

app.use(cors({ origin: '*'}));

But it shows me an reference error of require is not defined.