如何通过使用公共OData(读写)服务添加新记录?

2020-09-13 01:24发布

         点击此处--->   EasySAP.com群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


尊敬的SAP专家

我已经使用SAP WEB IDE Full-Stack工具创建了一个简单的UI5 Web应用程序。 我从 https://services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/Products ,数据将正确显示。

现在,我希望我也可以添加新记录。 我已经创建了表单,还有一个提交数据的按钮。 我还创建了如下控制器:

 handleSave:function(){
 var newData = {
 " odata.type":" ODataDemo.Product",
 " ID":this.getView()。byId(" InputID")。getValue(),
 "名称":this.getView()。byId(" InputName")。getValue(),
 "说明":this.getView()。byId(" InputDescription")。getValue(),
 " ReleaseDate":this.getView()。byId(" InputReleaseDate")。getValue(),
 " DiscontinuedDate":this.getView()。byId(" InputDiscontinuedDate")。getValue(),
 "评分":this.getView()。byId(" InputRating")。getValue(),
 "价格":this.getView()。byId(" InputPrice")。getValue()
 };

 var oModel = new sap.ui.model.odata.v2.ODataModel(" https://services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/");

 oModel.create(
 "/产品",
 新数据,
 {
 异步:是的,
 成功:功能(成功){
 alert(" success ..");
 },
 错误:function(oError){
 alert("失败..");
 }
 }
 );
 } 

但是它不起作用。 当我通过运行以下网址对数据进行计数时,数据数保持不变。 https: //services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/Products/$count

我也尝试了以下功能,但也无法正常工作。

 handleSave:function(){
 var newData = {
 " odata.type":" ODataDemo.Product",
 " ID":this.getView()。byId(" InputID")。getValue(),
 "名称":this.getView()。byId(" InputName")。getValue(),
 "说明":this.getView()。byId(" InputDescription")。getValue(),
 " ReleaseDate":this.getView()。byId(" InputReleaseDate")。getValue(),
 " DiscontinuedDate":this.getView()。byId(" InputDiscontinuedDate")。getValue(),
 "评分":this.getView()。byId(" InputRating")。getValue(),
 "价格":this.getView()。byId(" InputPrice")。getValue()
 };

 var requestObj = {
 requestUri:",
 方法: '',
 标头:{
 " X-Requested-With":" XMLHttpRequest",
 " Content-Type":" application/javascript",
 " DataServiceVersion":" 2.0",
 " MaxDataServiceVersion":" 2.0",
 " Accept":"应用程序/javascript"
 }
 };

 var url =" proxy/http/services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/";
 var method =" POST";
 requestObj.requestUri = url;
 requestObj.method =方法;
 requestObj.data = newData;
 OData.request(requestObj,function(){
 this.getView()。byId(" idAppControl")。getModel()。refresh();
 this.getView()。byId(" SimpleForm")。setVisible(false);
 });
 } 

请帮助我检查上述两个功能为何不起作用。

还有其他添加新记录的方法吗? 请告知。

         点击此处--->   EasySAP.com群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)


尊敬的SAP专家

我已经使用SAP WEB IDE Full-Stack工具创建了一个简单的UI5 Web应用程序。 我从 https://services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/Products ,数据将正确显示。

现在,我希望我也可以添加新记录。 我已经创建了表单,还有一个提交数据的按钮。 我还创建了如下控制器:

 handleSave:function(){
 var newData = {
 " odata.type":" ODataDemo.Product",
 " ID":this.getView()。byId(" InputID")。getValue(),
 "名称":this.getView()。byId(" InputName")。getValue(),
 "说明":this.getView()。byId(" InputDescription")。getValue(),
 " ReleaseDate":this.getView()。byId(" InputReleaseDate")。getValue(),
 " DiscontinuedDate":this.getView()。byId(" InputDiscontinuedDate")。getValue(),
 "评分":this.getView()。byId(" InputRating")。getValue(),
 "价格":this.getView()。byId(" InputPrice")。getValue()
 };

 var oModel = new sap.ui.model.odata.v2.ODataModel(" https://services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/");

 oModel.create(
 "/产品",
 新数据,
 {
 异步:是的,
 成功:功能(成功){
 alert(" success ..");
 },
 错误:function(oError){
 alert("失败..");
 }
 }
 );
 } 

但是它不起作用。 当我通过运行以下网址对数据进行计数时,数据数保持不变。 https: //services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/Products/$count

我也尝试了以下功能,但也无法正常工作。

 handleSave:function(){
 var newData = {
 " odata.type":" ODataDemo.Product",
 " ID":this.getView()。byId(" InputID")。getValue(),
 "名称":this.getView()。byId(" InputName")。getValue(),
 "说明":this.getView()。byId(" InputDescription")。getValue(),
 " ReleaseDate":this.getView()。byId(" InputReleaseDate")。getValue(),
 " DiscontinuedDate":this.getView()。byId(" InputDiscontinuedDate")。getValue(),
 "评分":this.getView()。byId(" InputRating")。getValue(),
 "价格":this.getView()。byId(" InputPrice")。getValue()
 };

 var requestObj = {
 requestUri:",
 方法: '',
 标头:{
 " X-Requested-With":" XMLHttpRequest",
 " Content-Type":" application/javascript",
 " DataServiceVersion":" 2.0",
 " MaxDataServiceVersion":" 2.0",
 " Accept":"应用程序/javascript"
 }
 };

 var url =" proxy/http/services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/";
 var method =" POST";
 requestObj.requestUri = url;
 requestObj.method =方法;
 requestObj.data = newData;
 OData.request(requestObj,function(){
 this.getView()。byId(" idAppControl")。getModel()。refresh();
 this.getView()。byId(" SimpleForm")。setVisible(false);
 });
 } 

请帮助我检查上述两个功能为何不起作用。

还有其他添加新记录的方法吗? 请告知。

付费偷看设置
发送
4条回答
昵称总是被占用
1楼 · 2020-09-13 01:58.采纳回答

您的请求的结构与实体集的结构不匹配,没有名称或描述字段。 使用浏览器开发工具查看错误。

此致

Jamie

2楼-- · 2020-09-13 02:11

杰米(Jamie)

感谢您的回复。 我已删除"名称和描述"字段。

但是现在我遇到了另一个错误,如下所示:

 datajs.js?eval:17 GET https://webidetesting1373339-s0019877056trial.dispatcher.hanatrial.ondemand.com/webapp/proxy/http/services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/ OData.svc/$ metadata 404
 请求@ datajs.js?eval:17
 E2 @ datajs.js?eval:17
 o.request @ datajs.js?eval:17
 (匿名)@ ODataMetadata-dbg.js:206
 a._loadMetadata @ ODataMetadata-dbg.js:149
 构造函数@ ODataMetadata-dbg.js:93
 构造函数@ ODataModel-dbg.js:280
 handleSaveTest @ ProductList.controller.js?eval:55
 a.fireEvent @ EventProvider-dbg.js:228
 a.fireEvent @ Element-dbg.js:504
 (匿名)@ ManagedObjectMetadata-dbg.js:644
 d.ontap @ Button-dbg.js:300
 a._handleEvent @ Element-dbg.js:231
 U._handleEvent @ UIArea-dbg.js:828
 派遣@ jquery-dbg.js:4737
 g @ jquery-mobile-custom-dbg.js:1972
 r @ jquery-mobile-custom-dbg.js:2091
 派遣@ jquery-dbg.js:4737
 c3.handle @ jquery-dbg.js:4549
 触发@ jquery-dbg.js:7819
 (匿名)@ jquery-dbg.js:7903
 每个@ jquery-dbg.js:365
 每个@ jquery-dbg.js:137
 触发@ jquery-dbg.js:7902
 P @ jquery-mobile-custom-dbg.js:1543
 R @ jquery-mobile-custom-dbg.js:1553
 派遣@ jquery-dbg.js:4737
 c3.handle @ jquery-dbg.js:4549
 ---- 

-----------------------------------------

 Log-dbg.js:414 2018-09-26 17:37:43.759899 [ODataMetadata]元数据的初始加载失败-
 c@Log-dbg.js:414
 L.error@Log-dbg.js:204
 错误@ Log-dbg.js:475
 l@ODataMetadata-dbg.js:74
 Promise.catch(异步)
 构造函数@ ODataMetadata-dbg.js:94
 构造函数@ ODataModel-dbg.js:280
 handleSaveTest@ProductList.controller.js?eval:55
 a.fireEvent@EventProvider-dbg.js:228
 a.fireEvent@Element-dbg.js:504
 (匿名)@ ManagedObjectMetadata-dbg.js:644
 d.ontap@Button-dbg.js:300
 a._handleEvent@Element-dbg.js:231
 U._handleEvent@UIArea-dbg.js:828
 dispatch@jquery-dbg.js:4737
 g@jquery-mobile-custom-dbg.js:1972
 r@jquery-mobile-custom-dbg.js:2091
 dispatch@jquery-dbg.js:4737
 c3.handle@jquery-dbg.js:4549
 trigger@jquery-dbg.js:7819
 (匿名)@ jquery-dbg.js:7903
 每个@ jquery-dbg.js:365
 每个@ jquery-dbg.js:137
 trigger@jquery-dbg.js:7902
 P@jquery-mobile-custom-dbg.js:1543
 R@jquery-mobile-custom-dbg.js:1553
 dispatch@jquery-dbg.js:4737
 c3.handle@jquery-dbg.js:4549 

为什么元数据加载过程失败?

此致

穆罕默德·塞普蒂扬迪

愤怒的猪头君
3楼-- · 2020-09-13 01:56

嗨Jamie

感谢您的回复。 我已删除"名称和描述"字段。

但是现在我遇到了另一个错误,如下所示:

 datajs.js?eval:17 GET https://webidetesting1373339-s0019877056trial.dispatcher.hanatrial.ondemand.com/webapp/proxy/http/services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/ OData.svc/$ metadata 404
 请求@ datajs.js?eval:17
 E2 @ datajs.js?eval:17
 o.request @ datajs.js?eval:17
 (匿名)@ ODataMetadata-dbg.js:206
 a._loadMetadata @ ODataMetadata-dbg.js:149
 构造函数@ ODataMetadata-dbg.js:93
 构造函数@ ODataModel-dbg.js:280
 handleSaveTest @ ProductList.controller.js?eval:55
 a.fireEvent @ EventProvider-dbg.js:228
 a.fireEvent @ Element-dbg.js:504
 (匿名)@ ManagedObjectMetadata-dbg.js:644
 d.ontap @ Button-dbg.js:300
 a._handleEvent @ Element-dbg.js:231
 U._handleEvent @ UIArea-dbg.js:828
 派遣@ jquery-dbg.js:4737
 g @ jquery-mobile-custom-dbg.js:1972
 r @ jquery-mobile-custom-dbg.js:2091
 派遣@ jquery-dbg.js:4737
 c3.handle @ jquery-dbg.js:4549
 触发@ jquery-dbg.js:7819
 (匿名)@ jquery-dbg.js:7903
 每个@ jquery-dbg.js:365
 每个@ jquery-dbg.js:137
 触发@ jquery-dbg.js:7902
 P @ jquery-mobile-custom-dbg.js:1543
 R @ jquery-mobile-custom-dbg.js:1553
 派遣@ jquery-dbg.js:4737
 c3.handle @ jquery-dbg.js:4549 
 Log-dbg.js:414 2018-09-26 17:37:43.759899 [ODataMetadata]元数据的初始加载失败-
 c@Log-dbg.js:414
 L.error@Log-dbg.js:204
 错误@ Log-dbg.js:475
 l@ODataMetadata-dbg.js:74
 Promise.catch(异步)
 构造函数@ ODataMetadata-dbg.js:94
 构造函数@ ODataModel-dbg.js:280
 handleSaveTest@ProductList.controller.js?eval:55
 a.fireEvent@EventProvider-dbg.js:228
 a.fireEvent@Element-dbg.js:504
 (匿名)@ ManagedObjectMetadata-dbg.js:644
 d.ontap@Button-dbg.js:300
 a._handleEvent@Element-dbg.js:231
 U._handleEvent@UIArea-dbg.js:828
 dispatch@jquery-dbg.js:4737
 g@jquery-mobile-custom-dbg.js:1972
 r@jquery-mobile-custom-dbg.js:2091
 dispatch@jquery-dbg.js:4737
 c3.handle@jquery-dbg.js:4549
 trigger@jquery-dbg.js:7819
 (匿名)@ jquery-dbg.js:7903
 每个@ jquery-dbg.js:365
 每个@ jquery-dbg.js:137
 trigger@jquery-dbg.js:7902
 P@jquery-mobile-custom-dbg.js:1543
 R@jquery-mobile-custom-dbg.js:1553
 dispatch@jquery-dbg.js:4737
 c3.handle@jquery-dbg.js:4549 

为什么元数据加载过程失败?

此致

穆罕默德·塞普蒂扬迪

大道至简
4楼-- · 2020-09-13 02:09

好像没有正确配置到服务的连接。 看看

https: //blogs.sap.com/2016/04/13/sapui5-application-using-odata-service-with-sap-web-ide /

此致

Jamie

一周热门 更多>

点击此处---> EasySAP.com 一起学习S4 HANA ...

相关问答