AngularJS controller makes ASP.NET Web Service return error code 500

After setting up an AMSX Web Service, everything seemed to be running smoothly when accessing the specified URL:

public class ExpenseService : System.Web.Services.WebService
{

[WebMethod]
    public void Get()
    {
        List<Expense> expenses = new List<Expense>();
        expenses.Add(new Expense(1, "Netflix", "Netflix", 20.00, new DateTime()));
        expenses.Add(new Expense(2, "Spotify", "Spotify", 14.90, new DateTime()));
        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(expenses));      
    }
}

https://i.sstatic.net/ymD9l.png

However, a problem arises when attempting to make a GET request from my AngularJS controller using $http:

var app = angular.module("app", ["ngRoute"]);

app.config(function ($routeProvider) {
    $routeProvider.when("/home", {
        templateUrl: "Pages/Home.html",
        controller: "homeController"
    })
});

app.controller("homeController", function ($http) {
var self = this;
$http.get("ExpenseService.asmx/Get")
     .then(function (response) {
         self.expenses = response.data;
     })
});

The console displays an error message as follows:

https://i.sstatic.net/lEDhT.png

Furthermore, clicking on the link within the error message (highlighted in the image above) results in encountering an error on the page:

https://i.sstatic.net/Jd9hH.png

It's worth noting that manual access of the JSON data through entering the URL in a browser still works fine.

Answer №1

[System.Web.Script.Services.ScriptService]//make sure to include this line before proceeding 
public class ExpenditureHandler : System.Web.Services.WebService

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 there a way to turn off step navigation in bootstrap?

Displayed below is a visual representation of the bootstrap step navigation component. Presently, there is an unseen 'next' button located at the bottom of the page. When this 'next' button is pressed, it transitions from 'step-1 ...

"Troubleshooting issue with setAttribute not functioning properly when used with onClick in

A new variable named "navBtn" is created by utilizing document.createElement("span") to generate a span element. However, for unknown reasons, applying setAttribute to this span is not functioning as expected. The navBtn variable resides within its own fun ...

The location of the THREE.Group object is consistently set at the origin (0, 0, 0) and errors may arise when attempting to accurately determine its position

In my Three.js project, I am utilizing a THREE.Group to manage selected dominoes within the scene. However, I am facing an issue where the reported position of the THREE.Group remains constant at (0, 0, 0) despite adding or removing objects to it. Addition ...

AJAX parsing through JSON files generated by PHP

Need help with sending a json to an ajax call and reading it when it's sent. Plus, the json structure seems off due to the backslashes... This is the ajax function in question: function find(){ var type = $('#object_type').val( ...

Is it possible to assign a different array to a variable in JavaScript?

I'm facing an issue with manipulating arrays in JavaScript within a function. This problem arises from an exercise found in the book Eloquent JavaScript, focusing on two specific functions: reverseArray(): designed to produce a new array that is the ...

Is there a way to retrieve the name of a class without the need to create an object instance or use a static method?

I find it frustrating to include the name of a class as a string parameter, such as "FileDownloader," in the code. I wish there was a way to reference the class name directly, like using FileDownloader.Name() where FileDownloader is the actual class name. ...

Tips for preventing duplicate properties in multiple C# classes

In my simplistic application, there are 10 tables, each containing a "CreatedDate" property which is a DateTime field indicating the date of creation. When retrieving data from the database, I use Data Transfer Objects (DTOs) to map the information. For e ...

Alter the class of the div element every three seconds

Greetings, I trust everyone is doing well. I am in need of some assistance from your brilliant minds. I have a circular div along with three CSS classes, and my objective is to switch the div's class and update the label text every 3 seconds. Any insi ...

What is the best way to send a JSON Object containing option parameters in order to open a new window?

Have you noticed the interesting parameter list for the window.open() object? Is there a possibility to use window.open({options..}); instead? What are your thoughts on this matter? ...

Unable to delete cookies that have been created through PHP

Let me explain how I handle cookies using PHP and Javascript. Firstly, in PHP, I create a cookie before the page is loaded using the following code: setcookie('my_key', $value, 0, ADMIN_COOKIE_PATH); For the Javascript part, I utilize jQuery a ...

I am encountering an issue where my useState value is coming back as undefined after I fetch data. It appears that the useState hook is

import React, { useEffect, useState } from 'react'; function MainContent() { const [objectsArr, setObjectsArr] = useState(null); useEffect(() => { async function fetchData() { let response = await fetch('http ...

The functionality for navigating the Angular uib-dropdown using the keyboard is currently experiencing issues

I am currently utilizing Angular Bootstrap 2.2.0 in conjunction with Angular 1.5. Despite enabling the keyboard-nav option, I am experiencing issues with keyboard navigation on UIB dropdowns. Below is the snippet of my code: <div class="btn-group" ...

Ways to display the values of angular variables within ng-bind-html

Converting HTML content from a database saved as varchar to front end JavaScript code can be tricky. The content is stored in a variable called vm.htmlContent and must be displayed as HTML on a web page. To achieve this, the ng-bind-html angular directive ...

IE experiencing error due to ExternalInterface.call

My menu screen is made up of separate flash movie buttons, each supposed to call a JavaScript function when clicked. While this works perfectly in Firefox, I'm experiencing issues in IE7 where it fails to execute. Unfortunately, I don't have acce ...

javascript execute while load

I'm having trouble initializing inputs with maps api autocomplete based on the number of inputs retrieved from the database. I'm trying to run a simple JavaScript function within a while loop, but it's not working as expected. Although the ...

Export an Object into a CSV Spreadsheet

I am trying to convert my object into a CSV table, but when I use this code sample, all I get is Systems.Strings[]. Here's the code snippet: Clear-Host #$zusammen = List of all UNIDs $zusammen = New-Object System.Collections.ArrayList($null) #Pat ...

What could be causing the React text input to constantly lose focus with every keystroke?

In my React project using Material-UI library, I have a component called GuestSignup with various input fields. const GuestSignup = (props: GuestSignupProps) => { // Component code goes here } The component receives input props defined by an ...

The constructor in a Typescript Angular controller fails to run

Once the following line of code is executed cockpit.controller('shell', shellCtrl); within my primary module, the shell controller gets registered with the Angular application's _invokeQueue. However, for some reason, the code inside t ...

node exports result in syntax errors

I have been facing issues while importing functionality from controller.js into my app.js. The syntax errors I keep encountering are: , expected statement expected Initially, I thought it would be simple to fix but every time I address one error, multi ...

What causes the break in Bamboo tasks once Protractor finishes?

Following the completion of my Protractor task, I am observing a 2-minute delay in my bamboo build. Below are the log details: [12/8/2015 5:10 PM] Agnew, Tyler: build 08-Dec-2015 16:45:24 Finished in 62.66 seconds build 08-Dec-2015 16:45:24 [32 ...