Angular phone directory. JSONP callback

Exploring the Angular textbook on the official website, I came across the PhoneCat phone list extracted from a JSON file in the directory.

I developed the server-side and deployed it to Heroku. The application generates valid JSON data.

How can I send a request to the server? Should I utilize the JSONP method? I have included the code snippet below which I believe should work, but unfortunately, it doesn't. Can someone please assist me?

This code works:

phonecatApp.controller('PhoneListCtrl', function($scope, $http) {
  var url = 'phones/phones.json';
  $http.get(url).success(function(data) {
    $scope.phones = data;
 });

This code doesn't work:

phonecatApp.controller('PhoneListCtrl', function($scope, $http) {
var url = 'http://MyServerSideOnHeroku.com/phones.json';
 $http.jsonp(url).success(function(data) {
   $scope.phones = data;
});

Why is this happening?

Answer №1

Remember to include "JSON_CALLBACK" in the url when using $http.jsonp!

Make sure to consult the documentation at https://docs.angularjs.org/api/ng/service/$http#jsonp

In your specific scenario, the Url should look like this:

var url = 'http://MyCustomServer.com/data.json?callback=JSON_CALLBACK';

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

How come my variable doesn't show up in the view even though I filled it in the controller?

I'm having trouble with my AngularJS setup. The angular.min.js file is located in the js folder, following a code example from O'Reilly's book. However, for some reason it is not working on my computer. //controller.js function HelloCont ...

Displaying items as objects in search results in Kendo Angular's auto complete feature

Seeking assistance with implementing Kendo Angular's auto complete widget using server filtering. Following the service call, the popup displays [object Object] and the count of these matches the results retrieved from the server. Could someone kindly ...

Toggle through the data being shown

I recently delved into learning AngularJS and I'm still getting the hang of it. One basic example that I'm working on involves toggling between displayed data in a table. Here's the basic setup of my app: <html> <head> ...

Protractor unable to locate elements using by.repeater

What is the best method for targeting this repeater with Protractor? <a ng-repeat="item in filteredItems = (items | filter:'abc')">{{item}}</a> ...

Jasmine tests for AngularJS directive failed to invoke the link function

I can't figure out why the link function of my directive isn't being called in a Jasmine test. I've created a simple example to illustrate. Here is the code for my directive (TestDirective.js): 'use strict'; angular.module(&ap ...

Changing HTML dynamically does not trigger the ng-click event within ng-bind-html

I have developed a custom directive that can display messages along with rendering HTML content that may contain Angular attributes like buttons, anchor tags with ng-click attribute, and more. index.html: <ir-error-panel status="status"></ir-err ...

Prevent removal of item from ngRepeat

Seeking a method to capture the removal of an element within an ngRepeat loop. My goal is to incorporate animations during this process. While handling the addition of elements can be done using the 'link' event, I am uncertain how to intervene d ...

Utilizing various layouts in ASP.NET MVC with AngularJS

I am setting up two different layouts, one for visitors and one for management. Routes: app.config(['$routeProvider', function ( $routeProvider) { $routeProvider .when('/', { templateUrl: 'Home ...

Is there a way to transfer controller scope to a partial HTML file?

Welcome to my HTML setup <div ng-controller="filterController"> <div class="quick-view" id="quickview" data-toggle="modal" data-target="#modal-bar" ng-click="quickView(quickview)"><i class="fa fa-eye"& ...

Is there a way to upload files in AngularJS without using AJAX or jQuery?

Currently, I am looking to create a gallery that allows for the uploading of multiple images. While I have come across some options that utilize ajax to immediately send the files, I prefer a solution that involves form submission. In addition, I would li ...

Using Angular Material for creating tabs with identical content

I am using material with angularjs and have encountered an issue with my two md-tabs. Both tabs have a similar DOM structure, but contain some unique content. Instead of duplicating the common DOM twice, I am looking for an alternative solution. Is it poss ...

Having trouble getting the templateUrl to work properly with AngularUI-Router?

Can you please explain the flow of how a URL is processed when visited and provide insights on why Angular's templateUrl may not be working? When a user clicks on a URL in their browser, it first checks the cache to see if the URL is saved from the $ ...

Can the combination of pure HTML+JS applications with .NET webservices be both feasible and safe?

Our recent projects have utilized MVC5 with AngularJS, Ninject, Bootstrap, and various other technologies. However, we found that these applications required significant time to fix bugs and add features due to unnecessary complexity. Would it be feasible ...

Is it possible for me to customize the default angular filter in order to prioritize displaying strings that begin with the search term?

In my current project, we are dealing with a massive list of strings (17,000+ at times) and have implemented an input box for filtering the list. Initially, I used Angular's default filter, but then a request came in to sort the list so that strings s ...

How can a TypeScript Angular directive utilize a function?

Recently, I have been following a unique Angular directive TypeScript pattern that I find really effective. The approach involves giving the directive its own isolated scope by creating a new controller. I encountered a scenario where I needed to invoke a ...

execute a function once the preceding function has finished running

When completing a success callback for an ajax query, the following tasks need to be accomplished: a) Updating the property of an object in an array (and locating that specific object within the array) b) Saving the updated array back into storage c) Bro ...

Swipe to modify Array

Currently, I am in the process of developing an application that features a Swipe card interface using both AngularJS and the Ionic framework. The functionality of this app will be similar to the one found at . When swiping to accept a card, I want the ar ...

Troubleshooting: Issue with Updating Views in Angular JS

I've been attempting to change the view from the controller, but it's just not working properly. app.js var app = angular.module('vla', ['ngRoute']); app.config(function ($routeProvider){ $routeProvider ...

Updating the fixed value and sending it to subordinate controllers in Angular version 1.4.9

I'm facing an issue with updating the value of a constant from the root controller. When the state transitions to a login controller, the constant still holds the old value. The CONSTANT is initially defined like this: var myApp = angular.module("ap ...

Transferring data to ng-model within ng-repeat loop

I am facing an issue with a form that is supposed to pass its inputs to ng-model before saving them into the database. One of the inputs is a dynamic value, specifically a pre-generated code retrieved from a database table. <div class="form-group" ng-r ...