Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have one query that, I have couple of json objects(contains id and description) in Postgresql database. My requirement is: I need to display each and every json data(id and description) from Postgresql database to my view page on hitting the URL automatically (like: if we hit http://localhost:9000 then that json data should come from database and should be displayed on my view page in table format on loading). Please let me know that how to implement it ? (Any examples or any code). Thanks in advance. In btw: I'm loading these json objects from localhost via jquery/ajax call to database/server.

//it is used to send json data to server

    $scope.loadJson = function() {
      var file, fr, input, receivedText;
      receivedText = function(e) {
        var lines, newArr;
        lines = e.target.result;
        alert('lines: ' + lines);//gives json data
        newArr = JSON.parse(lines);
        $.ajax({
          type: 'POST',
          contentType: 'application/json; charset=utf-8',
          dataType: 'json',
          url: '/sendJson',
          data: JSON.stringify(lines),
          success: function(data) {},
          error: function(XMLHttpRequest, textStatus, errorThrown) {

        });
      };
      if (typeof window.FileReader !== 'function') {
        alert('The file API isn\'t supported on this browser yet.');
        return;
      }
      input = document.getElementById('fileinput');
      if (!input) {
        alert('Um, couldn\'t find the fileinput element.');
      } else if (!input.files) {
        alert('This browser doesn\'t seem to support the `files` property of file inputs.');
      } else if (!input.files[0]) {
        alert('Please select file before clicking \'Load\'');
      } else {
        file = input.files[0];
        fr = new FileReader;
        fr.onload = receivedText;
        fr.readAsText(file);
      }
    };

html:

<table border="1"> 
                        <tr>
                            <td>
                                <label>Select File:</label>
                                <input type='file' id='fileinput' accept=".json" name="fileinput"> 
                            </td> 
                            <td>
                                <input type='button'  value='Load' ng-click="loadJson()">
                            </td>
                        </tr>
                    </table> 
share|improve this question
    
first place why you need $.ajax here in angular, you should use $http instead for making ajax – Pankaj Parkar 20 hours ago
    
@PankajParkar, thanks for your reply, yes we can use $http, but I am new to AngularJS, hopefully we can use both I guess. It is storing my files/objects into backend successfully. – Dhana 20 hours ago
    
you should use that.. It will messed up with angular digest system. and then you will face problem related to data binding. I'd recommend you go through this answer to clearing out things – Pankaj Parkar 20 hours ago
    
but, for me it is another Use case that belongs to send files to server. I gave it that for normal how I am sending to server. – Dhana 19 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.