Angular app sends a JSON request and receives no data in response

It seems like Angular may be loading the page before fully receiving all the information from JSONP. There are times when refreshing the page multiple times eventually displays the information, but it's not consistent. Interestingly, the code used on my projects' page does not encounter this issue.

HTML:

<div class="container">
    <div class="row push-top" ng-show="user">
      <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1">
        <div class="well well-sm">
          <div class="row">
            <div class="col-sm-3 col-md-2">
              <img ng-src="[[ user.images.138 ]]" alt="" class="img-rounded img-responsive" />
            </div>
            <div class="col-sm-7 col-md-8">
              <h4 ng-bind="user.display_name"></h4>
              <h5 ng-bind="user.occupation"></h5>

              <i class="fa fa-map-marker"></i>
              <cite title="[[ user.city ]], [[ user.country ]]">[[ user.city ]], [[ user.country ]]</cite>
              <br>
              <strong ng-bind="user.stats.followers"></strong> Followers, <strong ng-bind="user.stats.following"></strong> Following

              <hr>

              <p style="margin-top:10px;" ng-bind="user.sections['About Me']"></p>

            </div>
          </div>
        </div>
      </div>
    </div>
</div>

JavaScipt:

'use strict';

angular.module('angularApp')
  .controller('AboutCtrl', function ($scope, $window, Behance) {
    $scope.loading = true;
    Behance.getUser('zachjanice').then(function (user) {
      $scope.user = user;
      $scope.loading = false;
    }, function (error) {
      console.log(error);
      $scope.loading = false;
      $scope.user = null;
    });

    $scope.gotoUrl = function (url) {
      $window.location.href = url;
    };
  });

To view the problematic page, visit: . Appreciate any help in advance.

Behance service details provided below:

'use strict';

angular.module('angularApp')
  .factory('Behance', function ($http, $q, localStorageService, BEHANCE_CLIENT_ID) {

    // To clear data for testing purposes
    // localStorageService.clearAll();

    // Public API
    return {

      // Retrieve project list
      getProjects: function (config) {
        var pageNum = 1;
        if (angular.isObject(config) && angular.isDefined(config.page)) {
          pageNum = config.page;
        }

        var _projects = $q.defer(),
            _storedProjects = localStorageService.get('Projects_Page_');

        if (_storedProjects !== null) {
          _projects.resolve(_storedProjects);
        } else {
          $http.jsonp('https://www.behance.net/v2/users/zachjanice/projects', {
            params: {
              'client_id': BEHANCE_CLIENT_ID,
              'callback': 'JSON_CALLBACK',
              'page': pageNum
            }
          })
          .then(function (response) {
            if (response.data.http_code === 200 && response.data.projects.length > 0) {
              _projects.resolve(response.data.projects);
              localStorageService.add('Projects_Page_' + pageNum, response.data.projects);
            }
          });
        }

        return _projects.promise;
      },

      // Get project by ID
      getProject: function (id) {

        var _project = $q.defer();

        $http.jsonp('https://www.behance.net/v2/projects/' + id, {
          params: {
            'client_id': BEHANCE_CLIENT_ID,
            'callback': 'JSON_CALLBACK'
          },
          cache: true
        }).success(function (data){
          _project.resolve(data.project);
        });

        return _project.promise;
      },

      // Retrieve user info by username
      getUser: function (username) {

        var _user = $q.defer();

        $http.jsonp('https://www.behance.net/v2/users/' + username, {
          params: {
            'client_id': BEHANCE_CLIENT_ID,
            'callback': 'JSON_CALLBACK'
          },
          cache: true
        }).success(function (data){
          _user.resolve(data.user);
        });

        return _user.promise;
      }
    };
  });

Answer №1

Despite receiving no help from Anthony Chu, I managed to solve the problem on my own. It turned out that the issue was not with the Behance Service as previously mentioned, but actually with the Projects Controller.

The key change I made was switching $scope.loading from false to true within the service call. This adjustment consistently resolved the issue.

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

Switch the visibility of a div tag using Next.js

Script export default function Navigation(){ let displayMenu = false; function toggleMenu() { displayMenu = !displayMenu; } return ( <> <button onClick={toggleMenu}>Toggle Navigation</button> {/*This code sh ...

When Socket.emit is called, it outputs <span> elements within a well-structured message

Currently working on a small chat application using Node.js, Express.js, and Socket.IO. However, I'm encountering an issue with formatting. Instead of displaying the message content within <span> tags as intended, it is printing out the actual t ...

Is there a way to display the entire stack trace in Mocha when testing Promises and an error occurs?

Imagine I have a small specification like this: describe("feature", () => { it("does something", () => { return myPromiseBasedFn().then(result => { expect(result).to.eql(1); }); }); }); At the moment, when the promise is reject ...

Obtain one option from the two types included in a TypeScript union type

I am working with a union type that consists of two interfaces, IUserInfosLogin and IUserInfosRegister. The TUserInfos type is defined as the union of these two interfaces. export interface IUserInfosLogin { usernameOrEmail: string; password: string; } ...

What is the reason behind this being deemed as true?

Imagine we have this snippet of code: var attachRed = false; Why is attachRed = !attachRed equivalent to true? I'm curious because I'm working with Vue.js and trying to grasp why this particular piece of code functions as it does. <div id= ...

"Enhancing the user experience: Triggering a window resize event in jQuery before page load on Magento

I am trying to trigger this function before the page finishes loading, but currently it only triggers after the page has loaded. Can anyone assist with this issue? $(window).on('load resize', function(){ var win = $(this); //this = window ...

Insert a new element into a JSON array using C#

I have a JSON Array here and I am looking to append a new field into the array. However, I am unsure of how to iterate over it. { "data": { "pasca": [ { "code": "PDI1231", ...

The process of JavaScript for detecting and extracting text from

After much searching, I stumbled upon this intriguing piece of JavaScript code at this location. Curiously, I was able to remove a few eggs using the following script: var eggCoordinates = []; for (var i = 0; i < 5; i++) { eggCoordinates.push(new E ...

Exploring the features of NextJS version 13 with the benefits

Starting from the 13th step, SSR is utilized by default and in order to opt for client side rendering you must specify it at the top like so: 'use client' Currently, my setup involves TypeScript and styled-component integration. Take a look at ...

Express router is unable to process POST requests, resulting in a 404 error

I am having trouble fetching POST requests to my Express router. While my many GET requests work fine, this is my first POST request and it is not functioning correctly. Here is a snippet of my frontend code: export async function postHamster(name, age) ...

Generate HttpRequestHeaders using JObject

One of my challenges has been creating an API for my desktop software to streamline the process of changing HttpClient headers without having to constantly code and rebuild. My main hurdle now is figuring out how to generate a custom list of HttpRequestHea ...

Detecting server errors in Nuxt.js to prevent page rendering crashes: A Vue guide

Unique Context This inquiry pertains to a previous question of mine, which can be found at this link: How to handle apollo client errors crashing page render in Nuxt?. However, I'm isolating the focus of this question solely on Nuxt (excluding apollo ...

Troubleshooting jQuery.ajax - Why won't it function properly?

I've been struggling to get the ajax service functioning properly. I tried a simple $.get("http://google.com"), but it didn't work. Additionally, this code snippet failed as well: <html> <head> <script src="https://aja ...

The callback in Jquery getJSON does not execute even when a valid JSON response is received

My server is sending valid JSON objects (as verified by jsonlint.com) that have this structure: "{\"encryption\": {\"key\": \"gKV0oPJwC5CBQxmn\"}}" This is how my HTML file looks: <html> <head> <title&g ...

Contrasting outcomes when tackling a problem in node.js versus python

After tackling a challenging leetCode problem, I successfully came up with the following solution: Given d dice, each with f faces numbered from 1 to f, determine the number of possible ways (modulo 10^9 + 7) to roll the dice so the sum of the face up nu ...

Rearrange the layout by dragging and dropping images to switch their places

I've been working on implementing a photo uploader that requires the order of photos to be maintained. In order to achieve this, I have attempted to incorporate a drag and drop feature to swap their positions. However, I am encountering an issue where ...

When an item in the accordion is clicked, the modal's left side scroll bar automatically scrolls to the top. Is there a solution to prevent this behavior and

When I first load the page and click on the Sales accordion, then proceed to click on Total reported and forecasted sales, the scrollbar jumps back up to the top The marked ng-container is specifically for the UI of Total reported and forecasted sales He ...

Trigger an event upon completion of a write operation in AngularJS

I want to trigger a search after my user finishes typing (without hitting enter) in AngularJS. Here is a simplified version of my HTML: <div ng-class="input-append" ng-controller="searchControl"> <input type="text" ng-model="ajaxSearch" ng-cha ...

Generating a safe POST connection with express.js

Is there a simple method to generate a link for submitting a POST request using Express.js or a plugin? This approach can also be employed to enhance security for important actions like user deletion, including CSRF protection. In some PHP frameworks lik ...

Utilize Vue.js to send bound data to a click event

I am currently working with a v-for loop that iterates over data fetched from an API, setting the key as the ID of each object. My goal is to create a click event that captures the v-bind:key value of the clicked element. This will allow me to locate all t ...