jquery – 使用SAPUI5读取REST服务

前端之家收集整理的这篇文章主要介绍了jquery – 使用SAPUI5读取REST服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用SAPUI5访问REST服务.我在jQuery的帮助下发送了一个GET请求并期望 JSON响应,但我得到的只是一个空的JSON对象.但是,使用RESTClient测试的REST服务给了我正确的响应.

这是我使用sofar的代码

视图

  1. sap.ui.jsview("sapui5_test.SAPUI5_Test",{
  2.  
  3. getControllerName : function() {
  4. return "sapui5_test.SAPUI5_Test";
  5. },createContent : function(oController) {
  6.  
  7. var text = new sap.ui.commons.TextField( {
  8. width : "100%"
  9. });
  10.  
  11. // arrange controls on the page with a matrix layout
  12. var ml = new sap.ui.commons.layout.MatrixLayout( {
  13. columns : 2,layoutFixed : true,width : "500px"
  14. });
  15.  
  16. ml.addRow(new sap.ui.commons.layout.MatrixLayoutRow( {
  17. cells : [
  18. new sap.ui.commons.layout.MatrixLayoutCell( {
  19. content : [ text ]
  20. })]
  21. }));
  22.  
  23. var model = oController.initTodoModel();
  24.  
  25. text.setValue(model.getJSON());
  26. return [ ml ];
  27. }
  28.  
  29.  
  30.  
  31. });

调节器

  1. sap.ui.controller("sapui5_test.SAPUI5_Test",{
  2.  
  3. initTodoModel : function() {
  4. var oModel = new sap.ui.model.json.JSONModel();
  5. var aData = jQuery.ajax({
  6. type : "GET",contentType : "application/json",url : "http://sapm04.ibsolution.local:50000/demo.sap.com~d337_resttest_web/rest/todo/init/",dataType : "json",success : function(data,textStatus,jqXHR) {
  7. oModel.setData({modelData : data});
  8. alert("success to post");
  9. }
  10.  
  11. });
  12.  
  13. return oModel;
  14. }
  15. }
  16.  
  17. });

的index.html

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.  
  6. <script src="resources/sap-ui-core.js"
  7. id="sap-ui-bootstrap"
  8. data-sap-ui-libs="sap.ui.commons,sap.ui.table,sap.ui.ux3"
  9. data-sap-ui-theme="sap_goldreflection">
  10. </script>
  11. <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
  12.  
  13. <script>
  14. sap.ui.localResources("sapui5_test");
  15. var view = sap.ui.view({id:"idSAPUI5_Test1",viewName:"sapui5_test.SAPUI5_Test",type:sap.ui.core.mvc.ViewType.JS});
  16. view.placeAt("content");
  17. </script>
  18.  
  19. </head>
  20. <body class="sapUiBody" role="application">
  21. <div id="content"></div>
  22. </body>

如前所述,当我在RESTClient中运行与jQuery中相同的URL时,我得到一个填充的JSON对象,但是UI5页面中的结果是一个空的JSON obejct {}.

我也尝试了以下解决方案:

  1. var oModel = new sap.ui.model.json.JSONModel("http://sapm04.ibsolution.local:50000/demo.sap.com~d337_resttest_web/rest/todo/init/");

但这没有帮助.

解决方法

嗯,原因很明显.控制器中的return语句在json对象填充数据之前完成.那是因为$.ajax调用是异步的,这意味着JavaScript会对后端服务器进行调用,并且不会等到发送答案,而是直接转到下一条指令,该指令在oModel填充数据之前返回oModel .如果您向后端发出同步请求,您的问题将得到解决,您可以这样做:
  1. sap.ui.controller("sapui5_test.SAPUI5_Test",{
  2.  
  3. initTodoModel : function() {
  4. var oModel = new sap.ui.model.json.JSONModel();
  5. var aData = jQuery.ajax({
  6. type : "GET",async: false,jqXHR) {
  7. oModel.setData({modelData : data});
  8. alert("success to post");
  9. }
  10.  
  11. });
  12.  
  13. return oModel;
  14. }
  15. }
  16.  
  17. });

但是,建议不要使用同步调用,只需假设在调用完成之前暂停应用程序.对于少数请求而言,这可能不是很多,但如果您有一个需要与后端数据提供程序进行大量交互的大型应用程序,那么这将是一个主要问题.

如果由我决定,我会设计应用程序,以便我能够将回调函数注册到ajax请求中.因此,应用程序将遵循设计模式等责任链,当数据准备就绪时,依赖于它的模块将被执行,并且它不会停止应用程序的其他模块.

简单来说,通过调用success函数中的函数而不是在ajax调用之外,对数据执行任何操作.

猜你在找的jQuery相关文章