on 08-22-2014 2:59 PM
Hello All,
I am trying to use OData model and getting data from Northwind Odata service. I tried a number of examples here, but I am getting NO DATA message when I am running the application. I referred to code sample from different SCN threads, but I am not able to get data in my table. Can anyone help me?
When I checked in Chrome console, I am getting error that says - Failed to load resource: the server responded with a status of 501 (Not Implemented) & XMLHttpRequest cannot load http://services.odata.org/Northwind/Northwind.svc/$metadata. Invalid HTTP status code 501
Following is my code:
var uri = "http/services.odata.org/Northwind/Northwind.svc"; // local proxy for cross-domain access
// create OData model from URL
var oModel = new sap.ui.model.odata.ODataModel(uri, true);
sap.ui.getCore().setModel(oModel);
var oTable = new sap.ui.table.Table({ // create Table UI
columns : [
{label: "ID", template: "SupplierID"},
{label: "Company Name", template: "CompanyName", sortProperty:"CompanyName" },
{label: "Contact Name", template: "ContactName", sortProperty:"ContactName"},
{label: "Contact Title", template: "ContactTitle", sortProperty:"ContactTitle" },
{label: "City", template: "City", sortProperty:"City"},
{label: "Country", template: "Country", sortProperty:"Country" },
]
});
var f1 = new sap.ui.model.odata.Filter('Country', [{operator:"BT",value1:"S", value2:"V"}]);
var f2 = new sap.ui.model.odata.Filter('City', [
{operator:"EndsWith",value1:"r"},
{operator:"EndsWith",value1:"o"},
{operator:"EndsWith",value1:"n"}], false);
oTable.bindRows({path: "/Suppliers",
parameters: { select: 'SupplierID, CompanyName, ContactName, ContactTitle, City, Country' },
filters: [f1, f2]});
oTable.placeAt("content");
Thanks for all the help!
Ameya
Hi,
try with Ajax call. check this example JS Bin - Collaborative JavaScript Debugging</title> <link rel="icon" href="h...
Regards,
Chandra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Chandrashekhar,
Yes, it is working with Ajax call. The question is, why only with Ajax and why not the usual way? What difference does Ajax calls makes that fetches data while the usual way does not? Is this because of callign the northwind webservice or any other service issue?
Best Regards,
Ameya
HI Ameya
The main issue is that you are using a oData model and this service is not providing oData. I modified Chandra's jsbin to show how we are using JSONModel to fetch data.
-D
Hi Dennis,
Alright! So had this service enabled to provide OData, my code should have worked, correct? Also, if a service is not providing OData, fetching data with JSONModel and Ajax should be the preffered choice?
I can see that you are not making any Ajax calls to fetch the data. So basically, JSONModel is all I need in this case? If yes, then why make Ajax call? Parond me for asking multiple questions. I am just trying to get my understanding clear.
Thanks a lot for calrifying on this!
Best Regards,
Ameya
As Sakthivel mentioned the main issue is due to CORS and there are various ways to solve it. either running application by disabling SOP in chrome (javascript - Disable same origin policy in Chrome - Stack Overflow) or ways mentioned in this blog
Also please note that Northwind service is OData based. we can get response in json format by using $format=json
Regards,
Chandra
Hi Ameya,
This issue is no where related to the service & the service provides oData. As you don't proxy the request it ends up in CORS issue. So a HTTP OPTIONS request is triggered which ends up giving 501(Not implemented) error. An working example using oData model.
JS Bin - Collaborative JavaScript Debugging
I've used https://cors-anywhere.herokuapp.com/ to resolve the cors issue, but this is just for testing & not to be used in productive code.
Regards
Sakthivel
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sakthivel,
I tried using proxy as well. Here is the code that I used earlier.
var uri = "http://services.odata.org/Northwind/Northwind.svc"; // local proxy for cross-domain access
// create OData model from URL
var oModel = new sap.ui.model.odata.ODataModel(uri, true);
I agree this can be a proxy issue though! In that case, above code did not really proxy the service, correct?
Ameya
Hi Sakthivel,
Sorry I posted the wrong code, I tried using following code as well.
var uri = "/proxy/http/services.odata.org/Northwind/Northwind.svc";
I have defined organization proxy settings in Prerefences > network >proxy.
Any idea why this too did not work? What approach should be followed for Productive Systems?
Thanks again for giving dorection to this question.
Best Regards,
Ameya
It's just not about setting up the proxy. In UI5, they handle this by implementing a SimpleProxyServlet which you can find in web.xml if you've created your applications through eclipse using UI5 plugin. Even this is only for testing purpose
https://help.sap.com/saphelp_nw74/helpdata/en/2d/3f5fb63a2f4090942375df80abc39f/content.htm
I'm not aware of how to avoid CORS issues in productive code as we usually have all our services in the same domain.
Regards
Sakthivel
User | Count |
---|---|
87 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.