Encountering an Unexpected : Token When Parsing JSON Arrays in AngularJS

I've been searching on various forums for an answer to my AngularJS issue without any luck. I have a simple controller that calls an http service and reads the response data, which is in JSON format with an objects array. However, I'm facing difficulty accessing the 'results' array.

Here's the structure of the JSON output:

{
  "results": [{
    "id": "1",
    "download_url": "",
    "uploader": {
      "id": "114899"
    },
    "uploaded": "1442599380",
    "streaming_url_timeout": 1446092554
  }, {
    "id": "2",
    "download_url": "",
    "uploader": {
      "id": "114899"
    },
    "uploaded": "1442599380",
    "streaming_url_timeout": 1446092554
  }]
}

In my Service method that retrieves this JSON data:

this.getUData = function() {
  var deferred = $q.defer();

  $http.jsonp(API_URL + 'udata')
    .success(function(data) {
      deferred.resolve(data);
    })
    .error(function(data) {
      deferred.reject(data);
    });

  return deferred.promise;
}

This is how I invoke the service from my controller:

myservices.getUData().then(function(data) {
   $scope.uitems = data.results;
});

In the template:

<div class="item" href="#" ng-repeat="item in uitems">
  <h2>{{item.id}}</h2>
</div>

However, upon trying to access the items in 'results', I encounter the following error:

Uncaught SyntaxError: Unexpected token : 

The error seems to be originating from the line "results":[.

Answer №1

It appears that the callback function did not work properly on the PHP server in this instance. To circumvent this issue, I found a workaround by utilizing the 'GET' method within the $http service as shown below:

this.retrieveUserData = function() {
  var deferred = $q.defer();

$http({method: 'GET', url: API_URL + 'userdata'})
    .success(function(data) {
      deferred.resolve(data);
    })
    .error(function(data) {
      deferred.reject(data);
    });

  return deferred.promise;
}

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

Is it advisable to utilize media queries and transform: translateY(-%) to position content above the mobile keyboard?

I've been struggling for nearly a whole day and could really use some assistance. I'm having trouble understanding how chat web apps manage to work around this particular issue. There have been numerous forum posts discussing the problem: I am tr ...

Enhancing Bootstrap 5 with JavaScript

I am relatively new to creating webpages and have recently started working on one using Bootstrap 5. I am looking to dynamically add or remove the "bg-opacity" Bootstrap class from my navbar based on the viewport width. I initially attempted to achieve thi ...

What is the reason behind the inconsistent behavior of Javascript's Date.getDate() and .setDate() methods?

As a hobbyist coder, I'm facing a challenging problem with building a dynamic HTML/CSS calendar. My goal is to have cells filled in based on today's date. However, when I attempt to add days to fill in another 13 days by looping through HTML elem ...

Guide for implementing smooth fade in and out effect for toggling text with button click

I have this code snippet that toggles text on button click: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> function toggleText(){ ...

Transmitting video through a local area network

I am seeking a solution to stream video content to a local network with potentially over 1000 viewers. The streaming will need to be accessible via web browsers such as Internet Explorer, Chrome, and Firefox, as not all users have internet access due to co ...

$event.preventDefault(); failing to stop event propagation in Firefox

<li ng-repeat="chat in openedChats track by $index" ng-click="init($index,'maximize')" role="button"> <button class="minimized-chat-box" > <label style="float:left;color:white"><strong>{{chat.NAME}}</str ...

The array is acting strangely and not functioning as expected

I am currently working with express, node, and mongoose. When I access my mongoDB database and log it to the console, I get the following array: module.exports ={ stories: function(req, res, next){ Story.find(function(err, stories){ if(err) ...

What could be causing my AngularJS controller to fail in my jasmine test?

I encountered the following error message: "Controller: MainCtrl should retrieve a list of users and assign to scope.users FAILED TypeError: 'undefined' is not a function (evaluating 'Users.findAll()') at /Users/John/NetBea ...

Vue - Enhanced Image Cropper (Error: Unable to retrieve image result from this.$refs.cropper)

I have a crop function for Vue Advanced Cropper similar to the one below: crop() { const { canvas } = this.$refs.cropper.getResult(); if (canvas) { const form = new FormData(); canvas.toBl ...

What could be causing the discrepancy between my JSON response and my struct?

While testing my API on apiary, I encountered an issue. I created a struct for the response, but when I made the call and tried to conform it to my struct, I only received the header and not the body. My goal is to extract data about little league teams, p ...

Unable to access property 'map' of undefined - having trouble mapping data retrieved from Axios request

When working with React, I have encountered an issue while trying to fetch data from an API I created. The console correctly displays the response, which is a list of user names. However, the mapping process is not functioning as expected. Any insights or ...

Loading two scripts in sequence, the first utilizing the "src" attribute while the second is an inline script

The HTML code below shows two script elements being added to the body of a webpage. <html> <body> <script> const first = document.createElement("script"); first.setAttribute("src", "./remote.js"); first.async = false; const second = doc ...

Receive JSON data in URL as an array in PHP

What I aim to accomplish is: I have a JSON object structured like below var foo = { format:"json", type:"test", id:"26443" }; and my goal is to convert it into a URL in the following format 'http://example.com/a:3:{s:6:"format";s:4:" ...

InternJS - Unresolved TypeError: The undefined object does not have a property named 'readFile'

I am currently facing an issue with Intern.js during functional testing. The error mentioned in the title has me puzzled as I struggle to figure out how to successfully load json files through FS or require. Despite my best efforts and extensive searches o ...

Making an asynchronous request from jQuery to a Slim Framework API endpoint

I'm currently working on incorporating Slim into my project and I'm facing some challenges with setting up the AJAX call correctly: $.ajax({ url: "/api/addresses/", type: 'POST', contentType: 'application/j ...

What is the best way to implement the addMore event in my custom slot components when working with Vue Formulate?

I need help customizing the 'add more' button for group repeatable fields in Vue Formulate. I have created a custom slot component that is functioning correctly, but I am struggling to determine the click event needed to add another field when th ...

What is the process of sending a JavaScript request from a React application?

I have a button with the label "Show more". When this button is clicked, I want to load new objects dynamically without refreshing the page. const LoadMoreButton = ({ nextPageUrl }) => { const handleClick = e => { e.preventDefault(); const ...

Sleek transition-ready zoom functionality for THREE JS with clickable controls

Hey there, I've been attempting to animate a transition between scenes in THREE js for quite some time now. I have successfully cleared the scene and recreated the next one, but the transition between them is proving to be quite challenging. I have cr ...

The Autofocus feature in HTML5 is malfunctioning in Internet Explorer 9

I am currently working on a project for IE9, and I am having trouble with the HTML5 autofocus keyword not functioning as expected. Specifically, the autofocus feature that puts the cursor in the specified input field is not working in IE9 (and I am forced ...

What is the best way to retrieve a JSON key in ReactJS?

I am currently facing a rendering issue. In my componentDidMount function, I am using axios to make a GET call and then updating the state with the received JSON data. The problem arises when I try to access the keys of the JSON in the render method becau ...