0

I am getting empty response while reading json content from file please find below snippet which I am using in my code and json file from which I am trying to read data.

$http.get('content/json/environment.json').success(function(data) {
            if(data.hasOwnProperty('payload')){
                data = data.payload;
            }               
            deferred.resolve(data);
        });

environment.json file

 {
    "payload": {
        "environment": [
            {
                "environment": "Production"
            },
            {
                "environment": "QA1"
            },
            {
                "environment": "QA2"
            }
        ]

    }
}
7
  • 2
    use data.data Commented Feb 9, 2017 at 5:23
  • Hi , I tried now I am getting "Cannot read property 'hasOwnProperty' of undefined" Commented Feb 9, 2017 at 5:35
  • look your data.data in console.log(data.data) if it really return the json Commented Feb 9, 2017 at 5:43
  • ya getting undefined Commented Feb 9, 2017 at 5:47
  • $.getJSON( "content/json/environment.json", function( data ) {}); Commented Feb 9, 2017 at 5:56

1 Answer 1

2

Your response will be inside data.data

$http.get('content/json/environment.json').success(function(data) {
            if(data.data.hasOwnProperty('payload')){
                data = data.data.payload;
            }               
            deferred.resolve(data);
});

DEMO

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Sajeev, I tried now I am getting "Cannot read property 'hasOwnProperty' of undefined"
add a console.log and check if you can see the data console.log(data.data)
getting undefined
Thanks Sajeev, not sure why in project it is not loading like it.. even I added enviroment.json in public folder and trying to access it directly through URL but getting empty response.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.