Axios appends square brackets at the end of the parameter name

Utilizing vuejs in combination with axios and a Django server presents a challenge. The server requires parameters to be passed as travelers, but when using axios to send this data, it appends [] at the end resulting in travelers[].

Is there a way to prevent this behavior?

Here is an example snippet of the code:

Within the VueJS instance, we have:

data = {
        filter:{
            travelers:[],
            order:...,
...

The travelers variable can either be an array of ids or an empty array. Even though it is defined as travelers, when sent via axios, it adds [] automatically.

This part of the code generates the issue:

loadOrders: function () {
                        axios.get(this.list_orders_url, {
                            params: this.filter
                        }).then(function (response) {
                            app.orders = response.data;
                        }).catch(function (error) {
                            console.log(error); // todo
                        })
                    },

Effectively, it results in travelers[]=1&travelers[]=2 being included in the query string.

Answer №1

Utilize the paramsSerializer feature even with null indexes included

{
  params,
  paramsSerializer: { indexes: null }
}

Answer №2

To make sure everything runs smoothly, you need to define the serialization logic in the paramsSerializer property :) For this example, I opted for using query-string, but feel free to choose any other module or your own custom method.

const queryString = require('query-string');

loadOrders: function () {
                        axios.get(this.list_orders_url, {
                            params: this.filter,
                            paramsSerializer: params => queryString.stringify(params)
                        }).then(function (response) {
                            app.orders = response.data;
                        }).catch(function (error) {
                            console.log(error); // todo
                        })
                    },

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

Steps to set up gapi-script authentication with next-auth in a Next.js application

I have encountered an issue while working on my open-source project that involves utilizing the Google Drive API. Can anyone guide me on how to set up gapi-script authentication using next-auth in a Next.js project? I have managed to implement authentica ...

Remove input fields that were generated upon clicking a button

I'm facing an issue with my code that allows me to add inputs using @click="addInput()". However, I am struggling to delete specific inputs using @click="deleteInput(). When I try to delete an input with this.inputs.splice(index, 1), on ...

Exploring JSON data to extract values

I am facing difficulty parsing a complex array of JSON, specifically extracting the values "1238630400000" and "16.10". I need to extract all values from this JSON but I am unsure how to do it. Here is the code I have attempted so far: for (var key in my ...

Update class name in React component based on state change

My current setup involves the setting of active and class flags as shown below: constructor(props) { super(props); this.state = {'active': false, 'class': 'album'}; } handleClick(id) { if(this.state.active){ this.s ...

Leveraging the withWidth higher order component (HOC) provided by

I am currently using version 3.9.2 of Material UI and experimenting with the withWidth HOC in a server-side rendered application. When I disable Javascript in the debugger options of Chrome Developer Tools, everything functions as expected by displaying t ...

Adjust the node's location in Cytoscape.js

I recently switched from using Cola to fCose in Cytoscape.js for graphing multiple graphs with no connections. With Cola, I was able to manually set node positions by tweaking the layout options. However, with fCose, despite adjusting variables like quali ...

Creating a centered and beautifully styled picture with a specific maximum size using React

I recently completed the development of a new website, which can be viewed at Upon inspection of the website, it is evident that the photo is not centered and appears too large on mobile phones. Despite my efforts to align it using various methods outline ...

The React SwiperJs autoplay feature seems to be malfunctioning, as the swiper is not automatically

I have been utilizing the Swiper component in React from this link. However, I encountered an issue with setting it to autoplay as it doesn't auto swipe. Here is my attempted code: // Resource: https://swiperjs.com/get-started/ import React from &apos ...

PHP: Link to logo in different folder by including 'nav.php'

I am facing an issue with my nav.php file: <div> <!-- there's a lot of code here so I want to write it once and include it in all pages within the main folder as well as subfolders <img src="logo.png"> </div> The structur ...

Converting a file from a URL to a blob in PHP for use in JavaScript

Attempting to convert an image from a URL to a blob file that can be utilized in JavaScript, but encountering challenges. Is this achievable and if so, how? Current attempts include: // $request->location is the url to the file in this case an ima ...

What is the best way to pass an array to a JavaScript function from a different page?

My website has a static settings page where my JavaScript function uses AJAX to retrieve data from a MySQL table and display it in a table on an HTML document. It's working perfectly, gathering all the necessary data efficiently. Here's the code ...

The File is not being successfully sent to the controller in MVC due to AJAX returning an undefined value

I recently created an AJAXUpload method within a cshtml file in my C# MVC project: function AjaxUpload(url, method, data, successFunction, errorFunction, skipErrorDlg) { $.ajax({ contentType: false, processData: false, url: url ...

Check the dimensions of the image file entered using AngularJS on the client side

Before uploading an image to the server, I need to validate its dimensions on the client side. I have searched for solutions using img.Onload(), but that's not what I am looking for. All I want is for the user to choose the image from <input ...

Instructions for transferring an email attachment to a collaborative drive stored in a Google Sheets document

At the moment, I am utilizing a Google script that runs periodically on my Gmail account to retrieve all attachments labeled according to certain criteria. The issue arises when trying to access attachments within a folder located on a shared drive using t ...

Create a VueJs component and globally register it with a unique custom style

I am looking to integrate Vue-Select into my VueJs project by creating a WebPack configuration that exports separate files for vendors and pages. Vue-Select offers a lot of customization options, and I want to centralize these settings in a single file for ...

In Vue.js, passing an entire object through props seems to be causing issues, whereas passing just a single property works seamlessly

Currently, I'm experimenting with some practice code that involves fetching detailed card data by clicking on specific buttons displayed. I have successfully implemented the function to show detailed card information when you click one of the orange ...

Can you explain the variance between using 'npm i' and 'npm install'?

Can you explain the distinction between running npm i and npm install? Both commands are used to install all node Modules listed in the package.json file. While the purpose of both commands is clear - to install modules, there may be subtle differences be ...

Plugin refresh after callback

I have a webpage that features a row of buttons at the top, followed by some content below. Whenever one of the buttons is clicked, the content is updated using UpdatePanels. Within this content, I am attempting to incorporate the royal slider plugin, wh ...

"Troubleshooting Problem with JSON Encoding in PHP and Parsing with getJSON in

Apologies if this sounds like yet another discussion on the topic, but I've been struggling for hours without finding a solution. I'm attempting to retrieve data from a MySQL database, generate a JSON using PHP, and then parse this JSON in JavaS ...

Proper approach for mapping JSON data to a table using the Fetch API in a React.js application

Struggling to map some elements of JSON to a Table in Customers.jsx, but can't seem to figure out the correct way. How do I properly insert my Fetch Method into Customers.jsx? Specifically managing the renderBody part and the bodyData={/The JsonData/} ...