Utilizing AngularJS to make an API call with $http method and handling a

I am attempting to make a call to my webservice using "$http". When I use "$ajax", it works fine.

Here is an example of jQuery $Ajax working correctly:

$.ajax({
        type: "Get",
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:60237/api/Get/Work",
        data: { cdUser: 1},
        dataType: "json",
        success: onDataReceived,
        error: onDataError
    });
    function onDataReceived(data) {
    }
    function onDataError() {
    }

However, when I try to use Angular $htpp, it does not work:

app.controller('alertxxController', function ($scope, $http) {

    $http({
        method: 'GET',
        url: 'http://localhost:60237/api/Get/Work',
        params: 'cdUser=1'
    }).success(function (data) {
        alert("ok");
    }).error(function () {
        alert("error");
    });



});

I'm really struggling to understand why this is not working. I can't seem to identify the error as it returns a 404 (Not Found).

Answer №1

Your "params" setting needs to be adjusted.

Here's a corrected version:

$http({
        method: 'GET',
        url: 'http://localhost:60237/api/Get/Work',
        params: { userId: 1}
    }).success(function (data) {
        alert("Success");
    }).error(function () {
        alert("Error");
    });
});

Answer №2

My understanding of the $http syntax in angular framework is as follows:

$http.get('specify URL here').then(function (result) {
    // handle success response
}, function () {
    // handle error
});

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

Having trouble getting Node.js, express, socket.io, and ejs to work together?

Currently, I am working on improving my knowledge of java-script and had the idea to create a basic chat app using Express, stock.io, and ejs. Unfortunately, I am facing some challenges in getting it up and running smoothly. Below is the snippet from my ...

Top method for extracting mesh vertices in three.js

Being new to Three.js, I may not be approaching this in the most optimal way, I start by creating geometry like this: const geometry = new THREE.PlaneBufferGeometry(10,0); Next, I apply a rotation: geometry.applyMatrix( new THREE.Matrix4().makeRotation ...

Component not being returned by function following form submission in React

After spending a few weeks diving into React, I decided to create a react app that presents a form. The goal is for the user to input information and generate a madlib sentence upon submission. However, I am facing an issue where the GenerateMadlib compone ...

The error message in React Hook Form states that the handleSubmit function is not recognized

I'm facing an issue with using react-hook-form for capturing user input. React keeps throwing an error that says "handleSubmit is not a function". Any suggestions on how to resolve this would be highly appreciated. Here's the code snippet I&apos ...

Utilize Windows Phone to Encode NFC Tag

I am currently working on writing and reading NFC tags using the ProximityDevice class on Windows Phone 8.1. Here is the code snippet I'm using to write the tag... var dataWriter = new Windows.Storage.Streams.DataWriter(); dataWriter.unicodeEncoding ...

The LoginActivity(AsyncTask) encountered a NullPointerException

I've been trying to troubleshoot this issue, but I can't figure out why it's returning a nullpointer. Sometimes the response can be a successful json. public class Login extends Activity { public TextView loginErrorMsg; private ProgressDial ...

What is the most effective method for parsing a JSON object with a dynamic object name?

Is it possible to access specific object keys and perform actions based on the object name retrieved from a JSON object? If so, how can I achieve this? The JSON object 'x' will be fetched through an AJAX call from the server. Depending on the ob ...

Utilize PHP to extract JSON data from the db-ip.com API

I am encountering an issue with the db-ip.com API that is supposed to display information about visitor IP addresses. However, my current script only outputs < pre > tags without anything in between. I would like all parameters from the API to be dec ...

Implementing asynchronous loading of an image onto a webpage using JavaScript

Is it possible to asynchronously load an image specified in the src attribute of an HTML image tag? I am trying to invoke a Java class using an image src tag, but I want this to happen asynchronously without affecting the current functionality of the web ...

I must add and display a tab for permissions

I am currently using the material UI tab for my project. My goal is to display the tab only when the permission is set to true. I have successfully achieved this functionality, but the issue arises when the permission is false. It results in an error that ...

The result of Document.getElementById can show as "undefined" despite the presence of the element

Currently, I am tackling a project that involves extracting information from a website. I have opted to use the 'puppeteer' library in Node.Js for this task. However, I am encountering an issue where Document.getElementById is returning "undefine ...

"Encountered a problem while setting up the Mailgun webhook to handle both multipart and URL encoded

I have been working on creating a web hook listener for Mailgun, and I encountered an issue when I realized that Mailgun can post webhooks using either multipart or x-www-form-urlencoded content-types. Currently, my code uses Multer to handle multipart b ...

Steps to showcase a form on a webpage using a button

My webpage features an HTML table with a table navigation bar that allows users to add items or inventory. However, when the "add item" button is clicked, a form appears below the table instead of on top of it. I want the form to display itself right on to ...

Passport is raising a "missing credentials" error upon return

Hello everyone! I'm currently working on a password reset form and encountering an issue. When I submit the email in my POST form, I'm seeing a frustrating "Missing credentials" error message. This is preventing me from implementing the strategy ...

How can we solve the issue of missing props validation for the Component and pageProps in _app.js?

Below is the code snippet from _app.js for a nextjs project that uses typescript import React from 'react' import '../styles/globals.css' function MyApp({ Component, pageProps }) { return <Component {...pageProps} /> } export ...

Tips for Dynamic Importing and Rendering of Components in ReactJS

I'm looking to dynamically import and render a component in React. I have two components - Dashboard and Home. Essentially, I want to dynamically render the Dashboard Component inside the Home Component without having to import it beforehand or maybe ...

Using SVG graphics as data labels in a HighChart stacked column chart

I am attempting to generate a stacked column chart in Highcharts with SVG images as x-axis labels, similar to the image displayed here: https://i.stack.imgur.com/y5gL1.png I have managed to achieve this with individual data points per label (non-stacked ...

Why isn't the JavaScript if statement working properly when checking the length?

Below is an if statement that I have devised: var TotalMoney=0; var Orbs=0; if (TotalMoney.length==2) { Orbs+=1; } The intention behind this code snippet is to increase the value of "Orbs" by 1 when the digit length of "TotalMoney" equals 2. However, it& ...

Swapping out a section of text with AngularJs

Can you provide guidance on how to replace a string in AngularJs? I would like to achieve the following: {{string.replace('some', 'thing')}} Appreciate any help! ...

When making a jQuery + AJAX request, IE8 is causing a null return value

I'm completely stumped as to why this issue is happening. To start, the code has been checked and validated by the W3C validator as HTML5, except for some URL encoding problems (such as & needing to be &amp;), but I don't have the ability ...