Having trouble retrieving JSON data using http.get method, as the status returned is -1

I'm a beginner in AngularJS. I'm attempting to retrieve JSON data in my code using $http.get, but it's throwing an error and the status is showing as -1.

What could be causing this issue?

RecordApp.factory('recordaccess', ['$http', function($http) {
    $http.get('http://someurlforjson')
        .success(function(data) {
            return data;
        })
        .error(function(data, status, headers, config) {
            alert("An error has occurred. Status: " + status);
        });
}]);

Answer №1

The use of the deprecated $http legacy promise methods success and error has been discouraged. It is recommended to use the standard then method instead:

RecordApp.factory('recordaccess', ['$http', function($http) {
    return $http.get('http://someurlforjson')
        .then(function successCallback(response) {
            // This callback will be executed asynchronously
            // when the response is available
            return response.data;
        },
        function errorCallback(response) {
            // Executed asynchronously if an error occurs
            // or when the server returns a response with an error status.
            alert("An error occurred. Status: " + reponse.status + " - " + response.statusText);
        });
}]);

For more information, refer to the Angular Docs.

If this solution does not resolve the issue, there might be a problem with the JSON file path (incorrect path or access restrictions like CORS).

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

Automatically injecting dependencies in Aurelia using Typescript

Recently, I started working with Typescript and Aurelia framework. Currently, I am facing an issue while trying to implement the @autoinject decorator in a VS2015 ASP.NET MVC 6 project. Below is the code snippet I am using: import {autoinject} from "aure ...

Generating hierarchical JSON from a list of maps with multiple values in a Java program

In my code, I'm working with an ArrayListMultimap: ArrayListMultimap<String, String> fcmbuildProperties = ArrayListMultimap.create(); ArrayListMultimap<String, String> scm = ArrayListMultimap.create(); //HashMap<String, String> fcmb ...

Managing responses from ajax requests that can handle both text and json formats

After submitting a form via Ajax and receiving a response from the servlet in either text or JSON format, I'm wondering if there is a way to handle both types of responses. I've read through the jQuery/Ajax documentation, but the JQuery Ajax page ...

Interested in transferring an additional column value to the $scope.seriesSelected variable?

I've been delving into Timeline charts using the angularjs google chart API and have come across an interesting limitation. It seems that only 4 columns are allowed, with the extra column designated for tooltips. However, I have a specific requirement ...

Using Ionic to create a master-detail layout with a side menu

Trying to wrap my head around the master-detail (MD) pattern in Ionic with a side menu. The example code uses 'Playlists' as the master and 'Playlist' as the detail. The states are set up like this: .config(function($stateProvider, $ur ...

Determine the precise location of a screen element with jQuery

Can anyone help me determine the precise position of an element on the current visible screen using jQuery? My element has a relative position, so the offset() function only gives me the offset within the parent. Unfortunately, I have hierarchical divs, ...

What are the steps to integrate Laravel and AngularJS?

Exploring the integration of Laravel with AngularJS has lead me to ponder on the most effective way to structure such a project. Should I (A) opt for a single domain where an API is consumed directly from the Laravel project, or (B) have website.com and a ...

Stop jQuery from adding duplicate values to a table

When I make an AJAX call using jQuery and PHP to receive JSON response, I am encountering a problem with duplicate values. The code is functioning correctly, but when selecting an option from the drop-down list, duplicate entries appear. The scenario invol ...

Unable to use the same hexadecimal HTML entity in the value props of a React JSX component

Can someone explain why this code snippet displays dots as password and the other displays plain hexadecimal codes? <Field label="Password" value="&#x2022;&#x2022;&#x2022;&#x2022;&#x2022;" type="password" /> While this one disp ...

Is it possible to incorporate conditionals within a jade template using JavaScript?

I've been working on a Jade template that includes some logic, but I seem to be encountering an issue. Here's the code snippet: #container -for(var col=0; col < 2 ;col++){ - if(col % 4 == 0){ .movie_row - } ...

JavaScript is always overlooked and goes unused

Can you figure out why the Javascript code isn't working? <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <script type="text/javascript"> function ChangeDisplay() { alert("Changing"); document ...

My JavaScript functions are not compliant with the HTML5 required tag

I am currently developing input fields using javascript/jQuery, but I am facing an issue with the required attribute not functioning as expected. The form keeps submitting without displaying any message about the unfilled input field. I also use Bootstrap ...

How can one replicate the functionality of .transition from D3 within an Angular or React framework?

Everyone: I'm contemplating between using React JSX or Angular templates for data visualization instead of D3 DOM manipulation (e.g. .enter().append(), .exit().remove()). How can I achieve animation transitions like .transition().duration() in either ...

I am facing an issue with Angular where the $http.get method is

It seems like there must be a small oversight causing this apparently simple problem. I have a function that interacts with the Spotify API to search for an artist. I know that by accessing the corresponding route using a standard URL, a result is returne ...

I would appreciate it if someone could clarify how the rendering process occurs in React in this specific scenario

let visitor = 0; function Mug({name}) { visitor = visitor + 1; console.log(name, visitor); return <h2>Coffee mug for visitor #{visitor}</h2>; } return ( <> <Mug name={"A"}/> <Mug name={"B&qu ...

Parent's hover element

I am currently working with the following loop: <?php if( have_rows('modules') ): $counter = 0; while ( have_rows('modules') ) : the_row(); ?> <div class="col span_4_of_12 <?php if($counter == 0) { ?>firs ...

Using Jquery and Ajax to pass an extra PHP variable to a server-side script

I am working with a dropdown select box where the selected option is sent to a server-side script using Ajax. <select id="main_select"> <option selected="selected" value="50">50</option> <option ...

Unexpected disappearance of form control in reactive form when using a filter pipe

Here is a reactive form with an array of checkboxes used as a filter. An error occurs on page render. Cannot find control with path: 'accountsArray -> 555' The filter works well, but the error appears when removing any character from the fi ...

The Modal Textarea refreshes each time it is clicked

Whenever I try to type on the modal with 2 textareas, it stops and exits the textarea. The issue seems to be with onChange={event => setTitle(event.target.value)}, but I'm not sure how to fix it. <Modal.Body> <form onSub ...

Display a JSON encoded array using Jquery

Within an ajax call, I have a single json encoded array set: $var = json_encode($_SESSION['pictures']); The json encoded array is stored in a variable called "array" When I try to display the contents of "array" using alert, I get this respons ...