tips for sending the chosen product to axios

How can I send the selected item from the dropdown menu to Axios in order to retrieve data? I need to pass the item itself, not just the ID, to the API.

<label>City</label> 
<select @change="getArea()" v-model="key">
  <option :value='0'>Select City</option>
    <option v-for='data in cityList' :value='data.id'>{{ data.city }} 
  </option>
</select>

<script>
var self = this;
axios.get('http://172.31.0.114:5008/api/city/'+this.key) //I want to pass the selected item (text) to the API.
  .then(function(res) {
    self.areaList = res.data;
  })
  .catch(function(error){
    console.log('Error:', error);
  });
</script>

Answer №1

Give this a shot:

Retrieving Data with GET Method:

function fetchAreaData(e) {
  let selectedValue = this.value;

  axios
    .get("http://172.31.0.114:5008/api/city", {
      params: {
        city_id: 12345
      }
    })
    .then(function(response) {
      self.areaList = response.data;
    })
    .catch(function(err) {
      console.log("Error occurred:", err);
    });
}

Sending Data with POST Method:

function submitAreaData(e) { let selectedValue = this.value;

  axios
    .post("http://172.31.0.114:5008/api/city", 
      {
        city_id: 12345
      })
    .then(function(response) {
      self.areaList = response.data;
    })
    .catch(function(err) {
      console.log("An error occurred:", err);
    });
}

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

Utilize Jquery to calculate the total sum of values associated with a particular key in a JSON object based on input

I am facing an issue with my script where I am trying to sum up the clientPrice keys in a JSON object assigned to a form text element. Here is what I have: <input id="clientList" type="hidden" value="[{"clientID":"1","clientType":"0","clientPrice":"450 ...

Ways to alert user prior to exiting page without initiating a redirect

I have a feature on my website where users can create and edit posts. I want to notify them before leaving the page in these sections. Here's how I can accomplish that: //Add warning only if user is on a New or Edit page if(window.location.href.index ...

Generating a dynamic triangle within a div while ensuring it fits perfectly with the maximum width

I need help with creating a responsive triangle on a web page that takes three inputs. The challenge is to adjust the size of the triangle so it fits inside a div element while maintaining the aspect ratio of the inputs. For instance, if the inputs are 500 ...

Include a Class into a label using jQuery

I am having an issue where I need to specifically add the error class to a <label> with the name attribute set to choice_16_0. However, when I try to achieve this using my code, it ends up changing all labels on the page to <label for="choice_16_0 ...

Sending POST data to a PHP file without the use of jQuery results in AJAX malfunction

I have been struggling to make an ajax alert layer work with a POST method for the past few days, and I cannot figure out why it is not functioning. I have successfully used the same code structure to send form data through ajax using POST on other admin p ...

Exploring the Differences Between Meteor JS Backend and Express JS

I have a basic understanding, but I'm interested in learning more: I came across a comparison on Stack Overflow that likened Meteor JS and Express JS to oranges and potatoes. My current understanding is that Meteor JS is full stack (Front End, Back E ...

Deciphering the concept of promises within the Node.js platform

After some research, I have come to understand that there are three main methods of calling asynchronous code: Using Events, for example request.on("event", callback); Callbacks, like fs.open(path, flags, mode, callback); Promises While browsing through ...

As for the Pixel Art Creator feature, you can now switch between different modes and

I'm seeking assistance regarding my Pixel Art Maker project. I recently implemented two new features, toggle() and remove(). The issue I'm facing is that when I input numbers to create the grid and click the submit button, the grid is successfull ...

Angular factory transforming service

I am looking to transform the 'i18n' function into a factory in order to return a value instead of just setting it. Any suggestions or tips would be greatly appreciated! services.factory('i18nFactory', function() { var language = ...

WebWorker - Error in fetching data from server using Ajax call

I've been experimenting with making AJAX calls to an ajax.htm file using web workers. The goal is to have the data continuously updated at set intervals. Although I'm not seeing any errors and the GET request appears to be successful, the data i ...

Animating the loading of the step bar

Looking for some help with my progress bar created using Vue bootstrap components. I have set a default number in the data with the value: number, and now I want it to increase automatically whenever I navigate to the next page. Can anyone provide some g ...

Having trouble obtaining the serialized Array from a Kendo UI Form

I am working on a basic form that consists of one input field and a button. Whenever the button is clicked, I attempt to retrieve the form data using the following code: var fData = $("#test").serializeArray(); Unfortunately, I am facing an issue where I ...

The toolbar button in the Froala WYSIWYG editor is mysteriously missing from view

Embarking on a project with Froala editor and Angular 1, I meticulously followed the documentation to show the insertImage button and insertTable, but they are not appearing. Here is my code: var tb = ["bold", "italic", "insertTable", "insertImage"]; $sc ...

Removing a Div with Dynamic Parameters

I'm struggling to implement a feature in my form that allows the user to add multiple entries, but I'm having trouble with the removal aspect. Here is the JavaScript code: var i = 1; var divContent = document.getElementById ...

Tips for maintaining an active class on parent nav items in NextJS when navigating dynamic routes

In my nextjs-application, I've implemented a Navbar showcasing several navitems: Navbar.tsx: const Navbar = ({ navitems }) => { return ( <div> {navitems?.map((navitem, idx) => ( <NavItem key={idx} navitem={nav ...

Creating a customized function in javascript/jquery with the ability to override it

Currently, I am utilizing Visual Studio for writing JavaScript/jQuery. For instance, when I input: $('#selector').text('foo') and then highlight text and hit F12, Visual Studio directs me to the file jquery-2.2.3.intellisense.js, auto ...

Delete the element that was generated using jQuery

Is there a way to add and remove an overlay element using JavaScript? I am struggling to get JavaScript to remove an element that was created with JavaScript. Any suggestions? Check out this JS Fiddle example Here is the HTML code: <div id="backgroun ...

What are some ways you could utilize componentDidUpdate to make updates seamlessly without the need to manually refresh the page?

When looking at the data, the letter F represents the number of favorited movies and W indicates the number of watched movies. https://i.sstatic.net/HtGi2.png I have created a button that resets the count of favorited movies to 0. However, in order for t ...

Mastering the art of invoking a JavaScript function from a GridView Selected Index Changed event

In my current setup where I have a User Control within an Aspx Page and using Master Page, there's a GridView in the User Control. My goal is to trigger a javascript function when the "Select" linkbutton on the Gridview is clicked. Initially, I succe ...

Navigating through an object using both dot and bracket notation

Can anyone shed some light on why I keep getting an 'undefined' message when trying to access object properties using dot notation like return contacts[i].prop;? However, if I use bracket notation like return contacts[i][prop];, it works fine an ...