Skip to Content
0

Fiori QuickStartApplication: Load data from cross browser domain issue

Feb 02, 2017 at 04:50 PM

37

avatar image

I've setup a QuickStartApplication in the WebIDE and now I'm trying to get some JSON data from a remote repository that I want to display.
I'm struggling with the async AJAX call, because based on definition, if you're requesting data by cross-browser-domains, the Ajax setting async:false; will be ignored. Having that, the script (model/models.js) where I'm trying to load the data returns the model already but the data are still not available. Thus, I can not display anything in my view.

Any thoughts on this issue? How can I wait for the data before proceed?
In the example below, the data in the loadRepositoryModel function are still not available so that oModel.setData(data); tries to set "undefined".

sap.ui.define([
	"sap/ui/model/json/JSONModel"
], function(JSONModel) {
	"use strict";
	
	return {


	ajaxCallToCMISRepository: function() {
			jQuery.sap.log.info("Entering ajaxCallToCMISRepository");
			var resultObjects = "({})";
			var alfUser = "guest";
			var alfPass = "guest";
	    	var alfURL = "https://myserver:8443/repo/1.1/dev/root/area/51";
	    	jQuery.sap.log.info("Load JSON from Repository " + alfURL);
			var method = "GET";
			jQuery.sap.log.info("Before AJAX CALL");
			$.ajax
			({
				type: method,
				url: alfURL,
				async: false,
				data: "{}",
				crossDomain: true,
				dataType: "jsonp",
				beforeSend: function (xhr){ 
					xhr.withCredentials = true;
				    xhr.setRequestHeader("Authorization", "Basic " + btoa(alfUser + ":" + alfPass)); 
				},
				success: function (response){
			    	var jsonObjects = response["objects"];
			    	var countObjects = response["numItems"];
			    	for(var i = 0; i< countObjects; i++){
			    		var property = jsonObjects[i]["object"]["properties"];
			    		var propertyNameValue = property["cmis:name"]["value"];
			    	}
					resultObjects = jsonObjects;
			    	jQuery.sap.log.info("Return SUCESS of AJAX!");
				},
				error : function (xhr, status, error) {
					jQuery.sap.log.info("ERROR: " + xhr);
					jQuery.sap.log.info("ERROR: " + status);
					jQuery.sap.log.info("ERROR: " + error);
					var keys = $.map(xhr, function(item, key) {
						jQuery.sap.log.info("EE: " + key +  " ==> " + item);
					});
				}
			});
			return resultObjects;
		},
		
		loadRepositoryModel: function() {
			jQuery.sap.log.info("Entering loadRepositoryModel in model.js)");			
			var data = this.ajaxCallToCMISRepository();
			jQuery.sap.log.info("------------------------------------ >>" + data);
			//jQuery.sap.log.info("data:" + JSON.stringify(data));
	    	oModel = new sap.ui.model.json.JSONModel();
	    	//oModel.setData(data);
			oModel.setDefaultBindingMode(sap.ui.model.BindingMode.OneWay);
			return oModel;
		}
	};
});
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers