How to read data using AJAX call
Hi All,
In this blog I am going to tell you how to read data through AJAX calls.
Imagine that you want to use the data from some other site to your app .
Suppose you want to show news feeds in your app or you want to access some internet oData service like Northwind in your app. So one of the solution to your need is AJAX call.
So in this blog I am going to discuss :
1.Read XML data using AJAX (News feeds on this site : http://zeenews.india.com/rss/world-news.xml)
2.Read Northwind oData using AJAX (oData on this site: http://services.odata.org/V2/Northwind/Northwind.svc/Customers)
1.Read XML data using AJAX
Step 1: Create destination for the site (Not absolute URL)
Step 2: Make the destination entry in neo-app.json
{
"path": "/zeenews",
"target": {
"type": "destination",
"name": "zeenews"
},
"description": "News Feedservice"
}
Step 3: AJAX call .
First two steps just bring the data at the same domain.In this step you are actually going to call the URL and fetch the data into the model
var oModel = new sap.ui.model.xml.XMLModel();
var that = this;
var aData = jQuery.ajax({
type: "GET",
contentType: "application/xml",
url: "/zeenews/rss/world-news.xml",
dataType: "xml",
async: false,
success: function(data, textStatus, jqXHR) {
;
oModel.setData(data);
alert("success to post");
}
});
this.getView().setModel(oModel);
2..Read Northwind oData using AJAX
Yess, you can use the Northwind oData using AJAX call .
First two steps(“Create destination for the site” and ” Make the destination entry in neo-app.json”) are similar for Northwind.
Step 1: Create destination for the site
Step 2 : Make the destination entry in neo-app.json
{
"path": "/Northwind_New",
"target": {
"type": "destination",
"name": "Northwind_New"
},
"description": "wind connector"
}
Step 3: AJAX call.
I have used JSON model to access the Northwind oData.
onInit: function()
{
var oModel = new sap.ui.model.json.JSONModel();
var that = this;
var aData = jQuery.ajax({
type: "GET",
contentType: "application/json",
url: "/Northwind_New/V2/Northwind/Northwind.svc/Customers",
dataType: "json",
async: false,
success: function(data, textStatus, jqXHR) {
oModel.setData(data);
alert("success to post");
}
});
this.getView().setModel(oModel);
}
<Table id="idProductsTable" headerText="Comapany Info" class="sapUiResponsiveMargin" width="auto" items="{/d/results}">
.........
</Table>
Nice blog Raj.
Regards,
Ankur
Thanks Ankur
Nice blog!
One question, this is usefull only when you deploy on your SCP, as you are using destinations, how would you do, if you will deploy the app to your OnPremise gateway¿?
Hi,
Just try one thing , don't create destination instead of that write the complete url in "url" section
Like,
url:"https://example.com/abc.xml"
And make async true
Like,
async:true
Try this thing and do let me know
🙂
When trying this, I always have CORS problems...
Try this and let me know
Hi Rajvardhan,
Can we create destinations in Eclipse Mar2.0?
Hi Rajvardhan Thakare , firstly thanks for this useful blog post.
I’m also taking same CORS problem like Cristian Babei .
Do you have any idea?

Code is here;
Great blog.
TIP: Always make destination to avoid CORS header issue.
Hi Rajvardhan,
Instead of creating a new model, I'm trying to set a property in my global model to store the fetched data. But the property remains empty. Can you suggest how to resolve it?
Here is the code:
Hi,
setProperty() method won't work with your odata model.
Please create one json model at global level and try to set it to that model.
Please replace your code with below code snippet inside success call. I hope it will help.
This model will be created at global level.
And you can access the model using below statement
sap.ui.getCore().getModel("departmentModel");
I hope it will help.
Thanks
Hi Rajvardhan,
Thank you for your blog post.
I am using SAP BAS to generate my UI5 project using generators.
I don't have any neo-app.json file.
In such a case where do I define the destinations ? Is it manifest.json ?
Regards,
Sauranil
Hello, were you able to do the configuration in the manifest?
Hi Sauranil,
You can define the destination in xs-app.json file of your project.