Building a JSON table using Angular in JSFiddle

I've been struggling to make JSON and Angular.js work together, but so far no success. I'm trying to run the loadPeople function when the 'click here' link is clicked, but it doesn't seem to be working. I must be missing something trivial.

HTML

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Render json in table using AngularJs - jsFiddle demo by mjaric</title>


      <link rel="stylesheet" type="text/css" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">

      <script type='text/javascript' src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
      <script type='text/javascript' src="loadpeople.js"></script>

      <script type='text/javascript' src="http://code.angularjs.org/1.0.1/angular-1.0.1.min.js"></script>


  <style type='text/css'>
    table {
  border: 1px solid #666;   
    width: 100%;
}
th {
  background: #f8f8f8; 
  font-weight: bold;    
    padding: 2px;
}

  </style>

</head>
<body>
  <div ng-app="myApp">
<div ng-controller="PeopleCtrl">
    <p>    Click <a ng-click="loadPeople()">here</a> to load data.</p>
<table>
    <tr>
        <th>Id</th>
        <th>First Name</th>
        <th>Last Name</th>
    </tr>
    <tr ng-repeat="person in people">
        <td>{{person.id}}</td>
        <td>{{person.firstName}}</td>
        <td>{{person.lastName}}</td>
    </tr>
</table>
</div>
</div>

</body>


</html>

JS

var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([
    {
    id: 1,
    firstName: "Peter",
    lastName: "Jhons"},
{
    id: 2,
    firstName: "David",
    lastName: "Bowie"}
]));


var app = angular.module('myApp', []);

function PeopleCtrl($scope, $http) {

    $scope.people = [];

    $scope.loadPeople = function() {
        var httpRequest = $http({
            method: 'POST',
            url: '/echo/json/',
            data: mockDataForThisTest

        }).success(function(data, status) {
            $scope.people = data;
        });

    };

}

Answer №1

I successfully implemented your code in this fiddle and it worked flawlessly: http://jsfiddle.net/4w6Uv/3. Additionally, I tried the same example with a basic node.js server and made it work by modifying the mockdata to a simple array:

mockData = [{
    id: 1,
    firstName: "Peter",
    lastName: "Jhons"},
{
    id: 2,
    firstName: "David",
    lastName: "Bowie"}
];

Furthermore, it's important to note that jQuery is not necessary for your example (unless there is additional code using it that you have not shared). Since your post is not tagged with Angular, adding that tag could attract more responses. The function being called is provided by the Angular controller and does not rely on jQuery (apologies if this information is redundant).

Answer №2

Upon inspection, it appears that your success function is not being triggered. Have you confirmed that you are able to successfully make a POST request to '/echo/json/'?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the reason for rowsAffected not returning an integer value?

Currently, I am working on executing the SQLite statement in an iOS application. To verify the count of affected records, I have implemented success and error callback methods. Upon receiving the results, although the variables are correctly defined, I en ...

JavaScript does not recognize external styles (when an if statement is involved)

I recently started learning web development, and I encountered an issue while working on a responsive design project. When the hamburger menu is clicked, the lines for expansion that are defined in an external CSS file are not being detected. Instead, new ...

utilize saved JSON instead of Core Data

Many tutorials explain how to retrieve JSON objects from the internet and translate them into Core Data. Currently, I am developing an iOS app (with plans for Android as well) that fetches JSON objects from the web and presents them to users. Personally, ...

Is it more efficient to save a large object in AsyncStore or a local JSON file when using React Native?

I am developing a React Native application where I am fetching an object from an API call that contains approximately 4000 items, resulting in a slow 30-second response time. I am looking for a way to store this object locally so that it only needs to be ...

The getJSON function works perfectly on a regular page, but for some reason, it's not

I am encountering an unusual issue: I am trying to check if an email is already in use as a user enters it. It works fine on a simple page, but when I attempt to call it on a modal, it does not function. Below is the jQuery code: $('#user_email&ap ...

Exercises from the Shader Scriptures

This winter break, I have dedicated my time to delving into the world of shaders. I am currently faced with a challenging exercise: y = sin(x); Explore the exercises below and observe the results: Introduce time (u_time) into the equation before calcul ...

The functionality of jQuery touch events on iOS devices is not functioning properly

I'm encountering issues with jQuery touch events on iOS devices. Here is my current script: $(document).ready(function(){ var iX = 0,iY = 0,fX = 0,fY = 0; document.addEventListener('touchstart', function(e) { ...

Guide to dynamically rendering Material-UI dialogs based on certain conditions

Trying to implement a dialog box based on data returned from an Apollo hook, where I need to verify that one value matches an ID. If checker === true, the dialog should open automatically and close when the user clicks the Close button. const DialogComp ...

Manipulate SVG elements by dragging them along the boundary of the path

Looking to achieve a specific functionality where I can drag and drop an element along the edges of a drawn path, rather than inside the path itself. To clarify, the goal is to move the red marked element on the black line bordering the path, allowing move ...

"What is the purpose of using the `position: absolute` property for movement transitions while deleting an item from a list

Click here to see an example where items smoothly move in a list when one item is removed. To achieve this effect, the element needs to be styled with: .list-complete-leave-active { position: absolute; } I'm curious as to why it doesn't work w ...

Error encountered: JSON data is incomplete at the start of line 1, column 1

My project consists of a database, analysis.php, and index.php web pages. The analysis.php script retrieves data from the database, organizes it in a specific manner, and then displays it using json_encode($array); inside a div element with the id 'da ...

The appearance of Bootstrap-Navbar is altered when viewed on a mobile device

After creating a website that was compatible with both web and mobile devices, I encountered an issue where the navbar would not collapse on my smartphone. However, when I resized the browser window to match the size of the smartphone screen, it worked per ...

Creating PDF files for iPhone using Phonegap

While Phonegap does not have this feature in its API, iOS offers the capability through Quartz 2D. You can find more information about it here. How can I achieve similar functionality in Phonegap for iPhone? As a beginner, any guidance on setting up the n ...

Utilizing the powerful combination of AngularJS and Node.js functions

I am facing an issue with the key_client variable as it needs to be loaded with an md5 value obtained from a module called nodejs get-mac. Despite my efforts, I have been unable to make it work successfully. The problem seems to be that key_client is alw ...

Empty Json Index

I have developed an Ionic application that communicates with my Laravel-based API by sending JSON data. Whenever a post request is made to the designated route, I invoke a method on my controller. This controller then takes the received data and stores i ...

The OpenWeatherMap API is displaying 'undefined' as a response

I've been encountering some issues with my weather app project due to lack of clarity in my previous questions. To be more specific, every time I attempt to retrieve weather information in JSON format using the fetch method, it keeps returning undefin ...

Guide on retrieving a specific row with cUrl in PHP

Hi there, I've been using cUrl to communicate with an external web server and I'm receiving responses in HTML format. I managed to convert it into a JSON code with over 4000 rows, but now I'm struggling to extract the specified row containin ...

Using Puppeteer to Retrieve Data from a Web Page with Multiple Divs of the Same Class

I am in the process of creating a puppeteer script to scrape an announcements website. The challenge I am facing is how to extract the content of each page element with the same class using a loop. Upon inspecting the DOM, it's evident that all the co ...

Decoding a JSON array in VB.NET or C#

My web service provides JSON data in the following format: If login is successful: ["67", 1]; In case of failure: ["fail", "User input data is incorrect."] How can I properly deserialize this data? I have attempted the following: Dim serializer As Ne ...

Getting rid of interconnected models when generating numerous watched scopes in Angular

In my current database system, I have set up three models: Members, Positions, and Teams. Members can be assigned to one or more positions, while teams can have one or more positions assigned to them. The positions reference both members and teams. I have ...