cors - how to disable same origin policy in Javascript (Chrome) -
i designing sap ui5 application fetching data different server. currently, acing cross origin error while making odata calls , workaround setting --disable-web-security in chrome. want proper solution ( piece of code ) can used avoid error.
please note cannot make changes on odata server.
you have use proxy servlet fix cors issue. in config json of file manifest.json add links odata service or data provider e.g.:
"config": { "odataremote": "http://exampleservice.com/sap/opu/odata/sap/<servicename>", "odataproxyremote": "proxy/sap/opu/odata/sap/<servicename>", } in components.js url link , initiate odata model:
var oconfig = this.getmetadata().getconfig(); //get variables of config json defined in manifest.json // define if proxy required or not if (window.location.hostname == "localhost") { sserviceurl = oconfig.odataproxyremote; } else { sserviceurl = oconfig.odataremote; } when uri determined initiate odata model in components.js:
var oodatamodel = new sap.ui.model.odata.odatamodel(sserviceurl, mparameters); this.setmodel(oodatamodel, "odatamodel"); in web-inf folder need add following servlet web.xml:
<!-- ============================================================== --> <!-- ui5 proxy servlet --> <!-- ============================================================== --> <servlet> <servlet-name>simpleproxyservlet</servlet-name> <servlet-class>com.sap.ui5.proxy.simpleproxyservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>simpleproxyservlet</servlet-name> <url-pattern>/proxy/*</url-pattern> </servlet-mapping> <context-param> <param-name>com.sap.ui5.proxy.remote_location</param-name> <param-value>{protocol}://{host name}:{port number}</param-value> </context-param> the simple proxy servlet testing purpose only. if need productive environment need mature proxy servlet. recommend reverse proxy(e.g. apache http server), bring web , application servers in 1 domain.
you can check out sample provided sap simple proxy servlet
Comments
Post a Comment