Finding the IHttpActionResult Status Code in $http/Restangular: A Guide

In my Web API 2 POST method, I am returning IHttpActionResult

public IHttpActionResult PostSomething(string someStringParam)

If a certain condition is met, I return the following response:

return BadRequest("you are request sux l0l!!1!"); 

This response is handled by the error section of either the $http or Restangular call:

            .error(function(err) {
                //display failure notification or perform other action
            });

Edit: By "error", I mean the second function in

makeSomeDatabaseCall.then(function(resp){}, function(err){})

Is there a way to verify that the response type was "Bad Request" (or another specified type) and that the message matches exactly with:

"you are request sux l0l!!1!"

Answer №1

If you want to access the outcome in a more detailed manner, you can use the following method as an example:

private IHttpActionResult ObtainErrorOutcome(IdentityResult outcome)
        {
            if (outcome == null)
            {
                return InternalServerError();
            }

            if (!outcome.Succeeded)
            {
                if (outcome.Errors != null)
                {
                    foreach (string errorDetail in outcome.Errors)
                    {
                        ModelState.AddModelError("", errorDetail);
                    }
                }

                if (ModelState.IsValid)
                {
                    // There are no ModelState errors to send, so simply return an empty BadRequest response.
                    return BadRequest();
                }

                return BadRequest(ModelState);
            }

            return null;
        }

Just a heads up, this code snippet pertains to ASP.NET Identity functionalities.

Answer №2

The Error Function takes the XMLHttpRequest object as its initial parameter.

   .error(function(xhr, status) {
                     alert(xhr.status);
                });

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

When working with Vue JS, consider utilizing a data or computed property that is dependent on the value of a particular prop. In this case, the prop being altered is named "

While using pagination, I encountered an issue where the nextPage method works fine but the previousPage method throws an error. The error message warns against directly mutating a prop as it gets overwritten whenever the parent component re-renders. It s ...

Issue with managing cursor location using readline in Node.js

Currently, I am developing a console application in Node.js utilizing the readline module to manage cursor positions and obtain user input. Below is a custom library I have created for this purpose: // ReadLine.js const readline = require("readline&qu ...

Is there a way to create a customized calendar in Node.js?

I am looking for a way to showcase a calendar in a localized format, not only in terms of language but also supporting non-Gregorian calendars such as Persian, Chinese, or Buddhist. In the past, when I worked with Java, I relied on ICU4J for this task. Ho ...

Using an external function to implement Javascript and AJAX interactions

function makeAjaxRequest(destination_full_url) { if (window.XMLHttpRequest) {// code for modern browsers xmlhttp = new XMLHttpRequest(); } else {// code for old Internet Explorer versions xmlhttp = new ActiveXObject("Microsoft.XMLH ...

Load data from a MySQL Database into a DataTable

I am currently in the process of developing a CRUD application. With a large dataset stored in a MySQL database, my intention is to utilize a JQuery DataTable instead of manually creating a table. However, the issue I am facing is that while the table appe ...

The es6 object class is not defined

Hey there, I'm currently working on a simple pong game and encountering an issue with passing the player object into drawPlate. It seems to be printing the information correctly, but then I get hit with an Uncaught TypeError exception. The data print ...

Changing an array of arrays into an object using javascript

Here is an example array: var arrays = [ { "name": "aaa", "value": "bbb" }, { "name": "ccc", "value": "ccc" }, { "name": "ddd", "value": [ & ...

Ways to create a clickable image without any hovering disruptions

I'm currently working on my first website using CSS3, HTML, and JS. Once I finish this version, I plan to switch to bootstrap after ironing out the bugs. However, I've hit a roadblock and could use some help. Here is the js fiddle link: https:// ...

What is the best way to utilize a color picker to apply CSS color to a parent element?

When the pencil glyphicon is clicked, a color picker appears. I only want to apply the selected color from the color picker to the list elements that are the grand parent of the pencil glyphicon. Here's a snippet of my Ruby front-end code: <% cat ...

What is the best way to create an express web service using Selenium method with JavaScript?

I have been experimenting with a simple method using Selenium and JavaScript. My goal is to execute this method when I invoke a basic web service created with Express. Here is the Selenium method: async function example() { try{ let driver = aw ...

Stop node.js from automatically converting a nested object containing numeric keys into an array

Every time I send an object with a nested object containing keys that are numbers to my node.js server, the nested object gets converted into an array. Is there a way to prevent this from happening? Client: $.ajax({ url : `/cctool/report`, method ...

Launch an Android application directly from a web browser upon the webpage's loading

When a user visits www.example.com/myApp, I want my app to open automatically without any click required. I have attempted the following methods: window.onload = function () { window.location.replace("intent://something#Intent;scheme=myapp;packag ...

What steps should I take to fix an error in my code?

My current objective is to write a program that generates a square with dimensions of 200 pixels by 200 pixels. The square should be colored using specific RGB values: red (red value of 255), green (green value of 255), blue (blue value of 255), and magent ...

Troubleshooting the Issue of Tailwind CSS Failing to Build Accurately in a Next.js Application Launched

Currently, I am working on a project that involves a Next.js frontend situated in a client directory and a Node.js backend in a server directory. The project structure resembles the following: jukerabbit/ ├─ client/ │ ├─ pages/ │ ├─ comp ...

Adding a type declaration to the severity property in React Alert - A guide to Typescript

I've developed a type declaration object for the incoming data, but no matter what I try to define as the type for the property "severity", it's not happy. The options it wants (as displayed below) don't seem feasible. I'm curious if th ...

Navigating through webdriverjs elements until a specific text value is found is a useful technique in test

Can anyone help me understand how looping in WebDriverJs works with promises? Imagine you have the following html structure: <div id="textelements"> <span onclick="spanClicked('Rock')">Rock</span> <span onclick="s ...

Is the child component in Angular re-rendered or re-initialized?

Recently started working with Angular(14) and encountered a problem. Whenever I update a property of the parent component, which is an array, the child component gets re-initialized (running the ngOnInit function). This issue arises when using the child co ...

CellNav + Edit feature results in the grid taking over focus from elements located outside of the grid

I am currently dealing with an issue in my application where I am using ui-grid along with cellNav and setting editOnFocus=true for certain columns. The problem arises when the END_CELL_EDIT and CANCEL_CELL_EDIT events in the edit feature always trigger gr ...

Issue with Microsoft Azure and AngularJS: Chart not rendering data as expected

I've encountered an issue where I can view the data locally, but once I open the Windows Azure cloud service application, the Json data is no longer being passed into my chart (). The Console displays a message stating "Controller names should start w ...

Executing a directive function from an AngularJS controller

I am facing a challenge with integrating my controller and directive. In my controller, I have the following code snippet: .controller('MyCtrl', ['$scope', '$rootScope', 'restService', function ($scope, $rootSc ...