Performing a JavaScript POST request without using an API, where the payload contains multiple repeated

Seeking help with a Javascript POST request that is not an API request (axios or fetch), but an old classical request.

I require a payload such as

...&_selected_action=5&_selected_action=10
. Using a form submitting technique seems to be impractical in this case. I can handle a single occurrence like ...&_selected_action=5 successfully (referencing Aquajet @ pass post data with window.location.href), everything functions properly. However, I am struggling with sending the string for repeated values either manually or using URLSearchParams().toString().

Any suggestions on how to proceed?

Additional context: This request needs to be created in a Django+Vue application that replaces certain components of the traditional Django Admin system. The ListView is now being handled by Vue and I aim to reuse actions that require a Post request formatted as described above. While I can accomplish this with a single selected row, handling 2+ selected rows has proven challenging.

Answer №1

When considering my previous question, I've come to realize that the limitation of not being able to use form submitting methods may not be entirely accurate. As it turns out, an HTML form can indeed contain multiple fields with identical name=, and when submitted, it generates a post request as required.

However, a challenge arises when programmatically creating the form and attempting to fill it from a dictionary, as dictionaries cannot have duplicate keys.

To address this issue, I have made modifications to Aquajet's function detailed in this Stack Overflow thread. By checking if

params[key].constructor !== Array
, I convert non-array values to arrays containing a single element. I then iterate through these arrays, allowing for repeated values in both the parameter (in array format) and the form itself.

This approach resolves my initial problem, although I find the method of creating numerous hidden inputs for this purpose somewhat cumbersome. Therefore, I am keen on exploring alternative ways to construct the POST request.

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 fetching data using $http and promises in AngularJS

I'm having trouble connecting to my RESTful API within my AngularJS application. Despite my efforts, I'm not seeing any data being displayed on the screen. It seems like I might be misunderstanding the usage of $http with promises. Any suggestio ...

I have put in a lot of effort, but unfortunately, the JavaScript validation is still

<script> function checkInput() { var input1 = document.myform.ename.value; var input2 = document.myform.eid.value; if (input1 == "" || input1 == null) { alert("Please enter the user name"); } ...

Accessing a parent class constructor object in NodeJS from the Child Class

I'm currently working on creating a Controller Class that will handle the initialization of all my routes using ExpressJS. Below is a simple example of what I have so far: class Test extends Controller { constructor(App) { const Routes = [ ...

Exploring the attributes and functions of JavaScript Objects in an Angular environment

Can Angular's $http.post method recognize JavaScript object properties when sending data? To understand more about JavaScript objects and their properties, visit https://www.w3schools.com/js/js_objects.asp. Here is an example to illustrate my questio ...

Error encountered during NextJS build - file or directory not found for 500.html

I recently made the switch from Page Router to App router. Transitioning pages - 500.tsx to app - 500 - page.tsx However, I encountered an error when running yarn build/next build: > Build error occurred [Error: ENOENT: no such file or direc ...

Authentication of Django users with Azure Active Directory and application roles

Summary I am looking to incorporate Azure Active Directory for authentication and authorization in my Django web app. Inquiry Interested in understanding the process of integrating the msal library into a Django application, as most Azure examples are b ...

Exploring the population with embedded RxJs queries

I'm struggling to find a solution to this particular issue. Imagine there is an object type described as follows: Box { Fruit[n]: { Kinds[n]: { id: string; name: string; } } } From an API call, I received a bo ...

Creating functions within views

In my endeavor to develop a job board website, I am faced with the task of passing a zip code entered in a form to a search_results view (as zip_code). Within this view, my objectives are: To retrieve the surrounding zip codes within a specified mile rad ...

Establish a value for the attribute of an HTML tag

Can anyone assist me with setting the attribute value for the following HTML tag using Selenium WebDriver? For example, I need to change 50% to 70% either via JavaScript with WebDriver or a simple WebDriver script. I have tried several options but this pa ...

What is preventing this code from successfully altering the opacity of an element based on its id?

I'm curious as to why the element's opacity is not temporarily reduced when the corresponding button is pressed. Any assistance would be greatly appreciated. function pause(miliseconds) { var currentTime = new Date().getTime(); while (cur ...

The input value is displaying one value, but the output is showing a different value

I am encountering a unique issue and could really use some assistance. <input class="testVal" type="number" id="GT_FIRING_TEMPERATURE" value="-17" name="degC" onchange="angular.element(this).scope().unitConversion(value,name, id)"> Despite the valu ...

Issues with Autofocus in AngularJs on Firefox

Check out this straightforward directive to set autofocus: app.directive('autoFocus', function($timeout) { return { restrict: 'AC', link: function(_scope, _element) { $timeout(function(){ ...

Iterate through an array of objects to determine if there is a variation in any of the

I am trying to check if there is a discrepancy in the ages of objects within an array. $scope.myArray = [{name:'ted', age:'18', gender: 'm'}, {name:'bob', age:'18', gender: 'm'} ...

Struggling with transitioning from TypeScript to React when implementing react-data-grid 7.0.0

I'm trying to add drag and drop functionality to my React project using react-data-grid, but I keep encountering a "TypeError: Object(...) is not a function" error. I have a TypeScript version of the file in the sandbox as a reference, but when I try ...

Error: The reference to "google" has not been defined within the context of the Google Maps

Having trouble with Java script even though everything seems to be working fine. I am unable to run the code that follows, specifically the Google function which is crucial. Is there a way to remove this error or find another solution? //Error occurs on ...

Issue with Angular Promise ($q) functionality being non-operational

I am currently experimenting with integrating Angular and SignalR in a demo application. I have attempted to implement promises using the $q service, but for some reason my code is not functioning as expected. SERVICE var boardConsole = $.connection.buil ...

Displaying a dropdown menu from the top by utilizing the show() function

I've successfully implemented a dropdown menu using cssmenumaker. It works perfectly on desktop and mobile view. However, I'm facing an issue on mobile where the dropdown menu slides in from the left when clicked. I would prefer it to slide down ...

Is there a way to display the output of jQuery in an input box rather than a span?

I need to display the result of this function in an input box instead of a span tag because I plan to utilize the result in the following form. function checkChange(){ $.ajax({ url: "ordercalculate.php", data: $('form').seria ...

the Vue ref attribute is struggling to accurately determine the data type

Are you looking to include an additional property in a composeable method, but encountering an error stating property 'isActive' does not exist on type '{ id: string; text: string; }' Below is the code snippet: import { ref, type Ref } ...

Exploring Deeply Nested JSON Data using AngularJS

I'm encountering some difficulties while attempting to extract data from Behance.net. There are two specific issues that I am facing in retrieving the necessary information. The first challenge arises when trying to retrieve the project description. ...