Tips for sending all parameters in one object during an AJAX request

Looking for a more efficient way to handle my js code:

xmlhttp = GetXmlHttpObject();
if (xmlhttp != null) {
    var url = "/support/residential/outage/serviceOutage?cbrnum=" + cbrnum + "&btn=" + btn + "&outagetkt=" + outagetkt;
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("thankyoucontent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", url, true);
    xmlhttp.send(null);
}

My question now is, is there a way to combine all the parameters into one object, send that single object in the query string, and then retrieve the parameter values from that object in the servlet? Is this possible?

Answer №1

A simple and effective way to transmit information to the server is by using JSON:

let data = {username: username, password: password};
let apiUrl = "...?data=" + encodeURIComponent(JSON.stringify(data));

When handling this on the server side, make sure to extract the value of data and convert the JSON into usable data types (Transforming JSON in Python).

Just remember that most browsers have limits on URL length. If dealing with large amounts of data, consider using a POST request instead.

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

Is there a more efficient method for examining each multidimensional array?

I have created a program that converts JSON to PHP and checks every value of a multidimensional array with a foreach loop. If a value meets a certain condition, it deletes that key and value and then converts it back to JSON. I am trying to achieve this wi ...

Vuejs may encounter challenges with v-model when numerous items are present on the page

My simple page consists of a form at the top for submitting data and a list below that, as shown in the image: https://i.sstatic.net/3WjY2.png The list is populated with data from an api, each object having only 4 properties. Currently, there are a total ...

Performing a jQuery $.ajax request to a Web Api that accepts string arrays as parameters

I am attempting to use jquery's $.ajax to call a .net Web Api method. My variables are: var process = "first process", var nameArray = ["first", "second"], var valueArray = ["val1", "val2"] Then, I make the ajax call as follows: $.ajax({ url: json ...

Interacting with Object Arrays in ASP.NET MVC Using jQuery and JSON

I am currently dealing with an object that has 2 ArrayList properties. public class TestDTO { public ArrayList Test1 { get; set; } public ArrayList Test2 { get; set; } } When I return this object as JSON in my JsonResult Action, the SUCCESS from ...

Working with nested JSON data from the Spotify API in Python

Struggling to obtain a track ID from the search endpoint in Spotify. The data is deeply nested. If I use this code: results = sp.search(q='artist:' + 'Nirvava + ' track:' + 'Milk it', type='track') pprint.p ...

javascript resize canvas to fit content

Is there a way to crop an HTML canvas based solely on its content? I've been using strokeText to create the canvas content, making it tricky to determine the actual size. Would it be possible to loop through each pixel of the canvas, or is there a mo ...

Can calculations be performed on JSON fields in SQLAlchemy with Postgres?

I am currently working with sqlalchemy on a postgres database, attempting to perform arithmetic operations in a SELECT query involving two JSON fields representing floats. I have encountered difficulties in getting this functionality to work as intended. ...

Using AJAX to delete items from the personalized shopping cart on your WooCommerce website

I am encountering an issue with removing products from the cart. Although my code successfully removes the product in ajax, it fails to refresh the cart. Here is the code snippet I am working with: add_filter('woocommerce_add_to_cart_fragments', ...

What methods can I use to toggle between different divs using navigation buttons?

There are 4 divs on my webpage that I need to toggle between using the up and down arrows. Despite trying an if else statement, it doesn't work as intended. <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.c ...

Error: The bun.js application encountered a SegmentationFault at line 188

I'm attempting to execute the following command on my current repository, which already has a registry set up in the .npmrc. bun install -y The purpose of the -y flag is to generate the yarn v1 lockfile. However, it's resulting in the followin ...

Deliver notifications from server to client using socket.io

Attempting to send a message from a NodeJS server to a client using socket.io is my current challenge. During my research, I noticed a common practice across the web where the emit function is wrapped within io.on('connection', handler), and the ...

Spark: DataFrame separation by column value to write JSON files individually

If I have a DataFrame called df with user affinity data like this: user food affinity 'u1' 'pizza' 5 'u1' 'broccoli' 3 'u1' 'ice cream' 4 'u2' 'pi ...

The issue of the highlighted row not disappearing persists even after clicking on the adjacent table row

When selecting the gear icon for the menu option in a table row, I would like the background to turn yellow. I attempted the following code to highlight the table row: var view = Core.view.Menu.create({ model: model, menuContext: { ibmm: ibmm }, ...

Retrieve information from MySQL database to display in Android application

I am trying to retrieve data from a MySQL database using PHP and display it in an Android activity. I have coded it to pass a JSON Array, but I am encountering a problem connecting to the server because my database is located on a local server. Can someone ...

The some() method in Javascript with an extra argument

I want to validate if at least one element in an array satisfies a certain condition. The condition is based on a variable. Here's an example of how I'm currently attempting this: function checkCondition(previousValue, index, array) { retur ...

TestCafe has encountered an issue: "There are no tests available to run. This may be due to either the test files not containing any tests or the filter function being too

Attempting to run automated tests using TestCafe resulted in an error when executing the following command. testcafe chrome testc.ts The specified command was used to test the testc.ts file within my Angular application, with TestCafe installed globally ...

Vue.js build fails with JSON errors rendering a blank page

As I was building my first project to learn more about vue.js, I encountered an issue when using npm run build. It resulted in blank pages with errors related to JSON. However, when I used the typical npm run serve command, the page looked great and worked ...

Creating a variable name from a string using a function

I am creating a log to keep track of the functions that are called. One specific function I am using is record_activity( function_name ); I find it tedious to manually add this at the beginning of every function I want to monitor. Currently, my functions ...

Dual Image Flip Card Effect for Eye-Catching Rotations

In the process of enhancing a website, I am interested in incorporating a feature that involves multiple cards with both front and back sides (each containing separate images). Initially, the plan is to display only the front side of the card. Upon clickin ...

Tips on retrieving JSON data in a Bottle (Python framework) using a Jquery script

I'm currently facing an issue where I am sending a POST request with JSON data using AJAX jQuery, expecting to receive it on my API server built with Bottle. However, the JSON data is being sent from the client side but isn't being received by th ...