Skip to Content
0

Method model.loadData()

Mar 09, 2017 at 06:07 PM

88

avatar image

I am using the method model.load.Data(), but I need to consume other Odata service with username and password, How can I put the password and the username in the model.loadData()?

Bellow is my controller.js

sap.ui.define([
	'jquery.sap.global',
	'sap/ui/core/Fragment',
	'sap/ui/core/mvc/Controller',
	'sap/ui/model/Filter',
	'sap/ui/model/json/JSONModel'
], function(jQuery, Fragment, Controller, Filter, JSONModel) {
	"use strict";


	var CController = Controller.extend("sap.m.sample.InputStates.C", {


		valueHelpRequest: function(oEvent) {


			var model = new sap.ui.model.json.JSONModel();
			var newurl = "http://services.odata.org/Northwind/Northwind.svc/Categories";
			model.loadData(newurl, null, false, "GET", false, false, null);
			// Handling of both confirm and cancel; clear the filter
			var that = this;
			var handleClose = function(oEvent) {


				var oSelectedItem = oEvent.getParameter("selectedItem");
				if (oSelectedItem) {
					that.byId("InputValueHelp").setValue(oSelectedItem.getTitle());
				}
				oEvent.getSource().getBinding("items").filter([]);
			};


			// Create a SelectDialog and display it; bind to the same
			// model as for the suggested items
			if (!this._valueHelpSelectDialog) {
				this._valueHelpSelectDialog = new sap.m.SelectDialog("valueHelpSelectDialog", {
					title: "Categories",
					items: {
						path: "/value",
						template: new sap.m.StandardListItem({
							title: "{CategoryName}",
							active: true
						})
					},
					search: function(oEvent) {
						var sValue = oEvent.getParameter("value");
						var oFilter = new sap.ui.model.Filter(
							"CategoryName",
							sap.ui.model.FilterOperator.Contains, sValue
						);
						oEvent.getSource().getBinding("items").filter([oFilter]);
					},
					confirm: handleClose,
					cancel: handleClose
				});


				this._valueHelpSelectDialog.setModel(model);


			} else {
				this._valueHelpSelectDialog.setModel(model);
			}
			this._valueHelpSelectDialog.open();


		}
	});


	return CController;


});
ui5
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Gowrinath Gundu Mar 09, 2017 at 07:35 PM
1

Hi Rubens,

This is because your browser and odata service are in different origins.

Disable web security in your chrome browser and run the application. It should work.

Regards,

Gowrinath

Show 3 Share
10 |10000 characters needed characters left characters exceeded

How can I do it?

0

Hello Rubens,

Enter below line in your run

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

This should open chrome w/o web security

If it doesn't show up, change the user-data-dir to the directory path of your chrome

Hope this helps.

Regards,

Gowrinath

0

Hi Rubens,

Please mark the thread as answered if you are able to solve this.So it will ve helpful for others.

Regards,

Gowrinath

0
Gowrinath Gundu Mar 09, 2017 at 06:48 PM
1

Hi Rubens,

Looks like you want to access sample odata service. But you have declared JSON model in your code.

You should declare oData model as below

var url = "your service url"

var oModel = new sap.ui.model.odata.v2.oDataModel(url,true,"username","password");

Regards,

Gowrinath

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Now I get some errors in my console ...

The errors are:

OPTIONS http://.../sap/opu/odata/sap/ZGW_TRANSF_APPROVAL_SRV_02/zget_nameset/$metadata 401 (Unauthorized)


XMLHttpRequest cannot load http://.../sap/opu/odata/sap/ZGW_TRANSF_APPROVAL_SRV_02/zget_nameset/$metadata. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://webidetesting8084021-p...trial.dispatcher.hanatrial.ondemand.com' is therefore not allowed access. The response had HTTP status code 401.



Uncaught (in promise) Object {xmlDoc: document}


Uncaught (in promise) Object {message: "Failed to execute 'send' on 'XMLHttpRequest': Fail…W_TRANSF_APPROVAL_SRV_02/zget_nameset/$metadata'.", request: undefined, response: undefined}

0