我正在尝试使用 SAPUI5 访问 REST 服务。我在 jQuery 的帮助下发送了一个 GET 请求并期待一个 JSON 响应,但我得到的只是一个空的 JSON 对象。但是,使用 RESTClient 测试的 REST 服务给了我正确的响应。
这是我正在使用的代码:
看法
sap.ui.jsview("sapui5_test.SAPUI5_Test", {
getControllerName : function() {
return "sapui5_test.SAPUI5_Test";
},
createContent : function(oController) {
var text = new sap.ui.commons.TextField( {
width : "100%"
});
// arrange controls on the page with a matrix layout
var ml = new sap.ui.commons.layout.MatrixLayout( {
columns : 2,
layoutFixed : true,
width : "500px"
});
ml.addRow(new sap.ui.commons.layout.MatrixLayoutRow( {
cells : [
new sap.ui.commons.layout.MatrixLayoutCell( {
content : [ text ]
})]
}));
var model = oController.initTodoModel();
text.setValue(model.getJSON());
return [ ml ];
}
});
控制器
sap.ui.controller("sapui5_test.SAPUI5_Test", {
initTodoModel : function() {
var oModel = new sap.ui.model.json.JSONModel();
var aData = jQuery.ajax({
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) {
oModel.setData({modelData : data});
alert("success to post");
}
});
return oModel;
}
}
});
索引.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.commons,sap.ui.table,sap.ui.ux3"
data-sap-ui-theme="sap_goldreflection">
</script>
<!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
<script>
sap.ui.localResources("sapui5_test");
var view = sap.ui.view({id:"idSAPUI5_Test1", viewName:"sapui5_test.SAPUI5_Test", type:sap.ui.core.mvc.ViewType.JS});
view.placeAt("content");
</script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
</body>
如前所述,当我在 RESTClient 中运行与 jQuery 中相同的 URL 时,我得到一个填充的 JSON 对象,但是 UI5 页面中的结果是一个空的 JSON 对象 {}。
我还尝试了以下解决方案:
var oModel = new sap.ui.model.json.JSONModel("http://sapm04.ibsolution.local:50000/demo.sap.com~d337_resttest_web/rest/todo/init/");
但这没有帮助。