0

I have created a sample SAPUI5 application in online editor

Check this link to view my code

I am getting this below error -

> XMLHttpRequest cannot load
> http://services.odata.org/V2/Northwind/Northwind.svc/Products?$format=json.
> No 'Access-Control-Allow-Origin' header is present on the requested
> resource.

Can someone help me in solving this issue - I understand that it is CORS issue which can be solved by disabling the security in chrome.

However - I don't to disable the security feature of chrome. So, is there any other way (probably by passing some headers) to solve this issue.

Appreciate your help in advance.

P.S.: I have already looked in these links -

http://scn.sap.com/community/developer-center/front-end/blog/2013/06/29/solving-same-origin-policy-issue-in-different-ways

http://scn.sap.com/community/gateway/blog/2014/09/23/solve-cors-with-gateway-and-chrome

Rahul
  • 143
  • 2
  • 16

2 Answers2

1

add the northwind odata service as a destination under the DESTINATIONS tab of your hcp account. Then make an ajax call to the northwind services using relative path i.e. /destination/..... Or make your own java application that works as proxy which allows source as and re-routes the payload to the correct destination.

basically destination is also a proxy supplied for out of the box usage by sap

Rusheel Jain
  • 843
  • 6
  • 20
  • I am not using Hana hcp. However, I am just using an online editor for my sample app – Rahul May 27 '16 at 08:58
  • then unless you can request the odata service provider to add CORS headers in response, you will have to make a java application ( or any other backend application). Your UI will communicate with this java app instead of odata-service provider. In the java servlet, there exists doOptions() method which is called when CORS is performed. Here in response add the required headers. Then once pre-flight is approved the actual request will come to the doPost() method of your servlet. Here you will have to code for requesting the desired values from the odata service provider. – Rusheel Jain May 27 '16 at 10:33
0

If this is just for the purpose of testing, the following solution should be the easiest.

Use a proxy service which adds the needed headers, for example the following: https://github.com/Rob--W/cors-anywhere

Usage is really simple, just add the URL of the proxy service before your OData URL:

var uri = "https://cors-anywhere.herokuapp.com/https://services.odata.org/V2/Northwind/Northwind.svc/Products?$format=json";
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Marc
  • 6,051
  • 5
  • 26
  • 56
  • I'm trying this solutio but when accesing the URL https://cors-anywhere.herokuapp.com/https://services.odata.org/V2/northwind/northwind.svc/ it gives me the following message: "Missing required request header. Must specify one of: origin,x-requested-with". Please help, thanks. – Daniel Carpio Contreras Jan 30 '20 at 23:08
  • @DanielCarpioContreras opening the URL directly does not work. Making fetch calls does work. So using it as an OData model should also work. – Marc Jan 31 '20 at 08:54