当前位置: 首页 > Web前端 > HTML5

使用 SAP UI5 消费 OData 服务的一些常见错误和解决方案

时间:2023-04-05 22:38:07 HTML5

使用SAPUI5://localhost:8080'使用OData服务的一些常见错误和解决方案已被CORS策略阻止:RequestheaderfieldmaxdataserviceversionisnotallowedbyAccess-Control-Allow-Headersinpreflightresponse。原因是maxdataserviceversion这个与OData服务版本相关的字段,没有出现在server端配置的Access-Control-Allow-Headers数组中,这样就造成跨域CORS错误。maxdataserviceversion中可以看到这个字段Chrome开发者工具的网络选项卡:解决方案:app.all('*',function(req,res,next){res.header("Access-Control-Allow-Origin","*");res.header("Access-Control-Allow-Headers","*");res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");res.header("X-Powered-按",'9.0.0');next();});将Access-Control-Allow-Headers的值改为*。报错消失:报错2下面url对应的元数据在浏览器中无法正常显示。想要的结果是,在地址栏输入url后,我们可以看到http://localhost:8081/后面的元数据的实际内容,正常显示为xml格式:http://localhost:8081/https:/...$metadata?sap-language=EN一般情况下,请求的header字段为:Accept:application/xml根据expressresponseobject的API描述,如果send方法传入的参数是一个字符串,Content-Type自动设置为text/html:这个在Postman中可以观察到:我们可以在代理服务器的实现中使用res.set方法手动将Content-Type改为application/xml:这样,在浏览器中就可以正常显示了,现在通过代理服务器获取到的xml格式的元数据是:错误信息3/$batch资源只支持POST方法requestheader字段包含一个边界值:对于$batchoperation,Accept的值为multipart/mixed:YesJusthang:OData批处理请求通常用于消费者想要执行多个独立的HTTP调用并希望避免多次往返服务器的情况。SAPUI5中使用ODataModel发送批量请求的示例代码如下:vartmpModel=newODataModel("https://xxyyzz.com/sap/opu/odata/_SRV/",true);tmpModel.setDefaultBindingMode(sap.ui.model.BindingMode.TwoWay);tmpModel.setUseBatch(true);this.getView().setModel(tmpModel,"tmpModel");tmpModel.setDeferredGroups(["foo"]);varmParameters={groupId:"foo",成功:函数(odata,resp){console.log(resp);},error:function(odata,resp){console.log(resp);}};for(varm=0;m