Skip to Content
author's profile photo Former Member
-1
Former Member

JSONModel not loading from URL

I am trying to fetch JSON data from RESTFUL service when I use below code, loadModel() is not loading the data.

var oModel = new sap.ui.model.json.JSONModel();

//oModel.setData();

oModel.loadData("URL to my RESTFUL SERVICE");

Should I use .ajax call to load the data from RESTFUL Service similar to jQuery? in SAPUI5 too?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    Posted on Jul 02, 2015 at 05:04 PM

    what about?

    var oModel = new sap.ui.model.json.JSONModel("URL to my RESTFUL SERVICE");
    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Maksim Rashchynski

      You shoudln't rely in console.log(). I experienced that this method sometimes does not display the right value of the parameter, when the method was called.

      I advise you to debug your code. This way you'll see for sure what is in the model.

  • Posted on Jul 02, 2015 at 04:11 PM

    oModel.attachRequestCompleted(function() {

    //DATA IS READY HERE

    });

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 02, 2015 at 08:27 PM

    Hi Suhass,

    You are missing some more parameters on loadData, see below:

    oModel = new sap.ui.model.json.JSONModel("Url");

    oModel.loadData("Url",null,false);

    More Info - SAPUI5 SDK - Demo Kit

    Hope this helps.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 06, 2015 at 03:33 PM

    thank you all.. finally this is what I did. and it worked..

    var oModel = new sap.ui.model.json.JSONModel();
    data = oModel.loadData("<my resetful service>");
    
      oModel.attachRequestCompleted(function(data) {
          console.log("AddressModel: "+oModel.getData());
          oModel.setData(oModel.getData());
      });
    
    
      sap.ui.getCore().setModel(oModel,"addressModel");
    
    //I further used the addressModel in my UI elements to do binding.
    
    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 06, 2015 at 05:47 PM

    Thanks Maksim, that works, but I have moved the code setModel inside the attachRequestCompleted method.


    var oModel = new sap.ui.model.json.JSONModel();  
    data = oModel.loadData("<my resetful service>");  
    oModel.attachRequestCompleted(function(data) { 
       console.log("AddressModel: "+oModel.getData());  
    //      oModel.setData(oModel.getData());  --> removed.
     sap.ui.getCore().setModel(oModel,"addressModel"); 
      });  
    
    //I further used the addressModel in my UI elements to do binding.
    
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.