What is the method for confirming the returned response string value in Vue js?

I am having trouble checking the response values of a `Vue` API request. I attempted to use `resp.data == "-1"`, but it is not yielding the desired outcome. Can someone assist me in determining how to properly check the response value in `Vue.js`?

Vue.js function:

          callback: function () {
            this.$http
              .post(`/api/food-item/${data.ItemId}/delete`)
              .then((resp) => {
                if(resp.data == "-1")
                {
                  ShowError(this.$toast, "Used Items!!");
                }
                else
                {
                  ShowSuccess(this.$toast, "Food item deleted successfully");
                  resp.data;
                  self.$nextTick(function () {
                    self.$refs.vuetable.refresh();
                  });
                }
                
              })
              .catch((error) => {
                const errorMsg = error.response.data.ExceptionMessage;
                ShowError(this.$toast, errorMsg);
              });
          },

C# code:

        [Route("{foodItemId}/delete")]
        [HttpPost]
        public IHttpActionResult Delete(int foodItemId)
        {
            if (!foodItemService.IsExist(foodItemId))
            {
                return NotFound();
            }
            if (foodItemService.IsUsed(foodItemId))
            {
                return Ok("-1");    //this line is the return line
            }

            foodItemService.Delete(foodItemId);
            return Ok();
        }

Error encountered in the terminal:

  312:19  error  Replace `(resp.data·==·"-1")␍⏎···············` with `·(resp.data·==·"-1")`  prettier/prettier
  315:18  error  Replace `␍⏎················else␍⏎···············` with `·else`              prettier/prettier
  323:18  error  Delete `␍⏎················`                                                 prettier/prettier

Answer №1

Vue Resource's documentation on Github states that in order to properly access the response data, you should use response.body rather than response.data:

if(resp.body == "-1") {
    ShowError(this.$toast, "Used Items!!");
}

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

Explore the power of Infinity.js for optimizing the rendering of large HTML tables, complete with a detailed example using prototype

Is there a way to efficiently load/render large html tables without compromising performance, especially in Internet Explorer? I recently came across a plugin in prototype.js (https://github.com/jbrantly/bigtable/, post: , demo:) that addresses this issue ...

Labels can sometimes cause text input fields to become unresponsive

I've encountered a bug while working on my website with the materializecss framework. Sometimes, inputs are not responding correctly. This issue seems to occur when clicking on the first input and accidentally targeting the higher part of the second ...

Using jquery.ajax to post to a single page without passing any variables

In order to capture the onclick event for a specific #id, I am retrieving form data and checking if $_POST['submit'] is set. If it is, I proceed to send an email. Although there are no errors, it appears that no data is being sent via post. < ...

What could be causing my Vuetify Stepper Component to be hidden?

I am utilizing Vue and Axios to display data using the Stepper Component from Vuetify. However, after adding the API data, it seems that the CSS class is somehow being set to display: none;. I am confused as to why this is happening, so any help would be g ...

How can I show a div beneath a row in an HTML table?

Check out the code snippet below: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <style type="text/cs ...

An error occurs even before any Components are rendered, with a TypeError stating that the property 'type' cannot be read because it is undefined

I'm facing an issue with my Redux App where the first component (App.js) is not rendering due to continuous errors. The reducer mentioned below is being triggered at some point without any action, causing the compiler to interpret action as null and f ...

Having trouble with the functionality of the jQuery `has()` method?

Consider the following HTML code snippet: <div class="comment"> <span class="test">some content here</span> <p>Lorem ipsum</p> </div> <div class="comment"> <p>Lorem ipsum</p> </div> The ob ...

Enhance D3 Version 6 Stacked Bar Chart with Spacing and Interactive Features

My bar chart lacks the spacing between bars that I intended to achieve, and the interactive tooltip doesn't show up when hovering over the bars. I could use some assistance with this. The purpose is to display the value of a specific color when hoveri ...

Emphasize the importance of paying attention to read-only

Currently, I have a form containing three inputs. The last input has the property of being ‘readonly’, as the user is supposed to fill it by clicking buttons (such as a virtual keyboard) rather than typing manually. Everything functions correctly in th ...

Caching AngularJS Templates

My current challenge involves generating templatecache using gulp-angular-templatecache and merging it with an Angular script into a single JS file. Here is the gulp task setup: gulp.task('generatetemplatecache', function () { return gulp.s ...

Transferring information from a server action to a server component

Is there a way to transfer data from my server action, specifically the value of the temperature variable, to the Home server component in my nextJS14 app? I want to display this value in the jsx of my Home server component. How can I achieve this? impor ...

Incorporating an Edit button with an icon into a react-bootstrap-table2

Is there a way to insert buttons in the Edit column of my table so I can easily edit a row? I believe there should be a method to personalize the column and incorporate icons as shown in the example image. Sample Image of What I want to do: import React ...

Is it time to execute a mocha test?

Good day, I am currently exploring the world of software testing and recently installed Mocha. However, I seem to be encountering an issue with running a basic test that involves comparing two numbers. Can someone please guide me on why this is happening a ...

Revolutionary scroll-based navigation fill transition

I'm attempting to achieve an effect where the fill color of the "insticon" SVG changes from black to white based on the scroll position indicated in the jQuery code. I have made the necessary modifications in the if and else statements, but unlike "he ...

nextAuth.js is failing to access the accessToken, returning only the values {iat, exp, jti} instead

Here is the code I am working with: import NextAuth from "next-auth" import CredentialsProvider from "next-auth/providers/credentials" export default NextAuth({ sectret:process.env.NEXTAUTH_SECRET, session: { strategy: "jw ...

Scraping data from a webpage using Node.js and the Document Object Model

I am attempting to extract information from a specific website using Node.js. Despite my best efforts, I have not made much progress in achieving this task. My goal is to retrieve a magnet URI link which is located within the following HTML structure: < ...

What is the best way to send multiple id values with the same classname as an array to the database via AJAX in Codeigniter?

Hey everyone, I'm facing an issue where I need to send multiple IDs with the same class name but different ID values to the database using AJAX. However, when I try to do this, only the first value is being picked up and not all of them. How can I suc ...

javascript touch scroll effect

I am looking to enhance the scrolling capability of my app to make it touch-friendly. I have already implemented scrolling using the mouse wheel, drag, and click functions. I recently tried using the touch-scroll plugin from touch-scroll to enable touch sc ...

What is the best way to display items within a table using React?

I'm just starting to learn React. Can someone show me how to use the "map" function to list elements from two different arrays in two columns? state = { dates: ["2000", "2001", "2002"], cases: ["1", "2", "3"] } render() { return ( <thea ...

Is there a way to enable scrolling on the page upon loading, without moving the embedded PDF object itself?

After embedding a PDF object on my webpage, I noticed that when loading the page and scrolling using the mouse wheel, it actually scrolls up and down inside the PDF instead of moving through the entire page. To fix this issue, I have to first click in a bl ...