Is it possible to transmit parameters when calling an ajax function on an HTML page?

I am trying to implement multiple buttons that trigger the same ajax function. Each button should be able to pass its own unique ID so that the ajax function can apply its action on a specific element related to that button. Is it possible to include a parameter with the function call? For example, when button 1 is clicked, I want the ajax function to modify textbox 1. Similarly, if button 2 is clicked, the function should change textbox 2 and so on.

Answer №1

If you need to pass parameters, you have options. One way is by adding them to the URL (using GET), or you can send them in the request body (via POST). Here's an example:

var getId = 1;
$.get("http://my.web.site/getaction/" + getId, function(result){
    console.log(result);
});
var jsonData = {key: "value"};
$.post("http://my.web.site/postaction", jsonData, function(result){
    console.log(result);
});

If the parameter is simple and doesn't require encryption, using GET is sufficient. However, if the parameter is complex or sensitive information like a password that needs to be encrypted, it's best to use POST and include it in the request body.

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

Struggling to figure out the correct way to use User.findOne({ userID: searchUserID }? I'm not receiving a return value at

Currently, I am facing some issues while trying to implement an update/put functionality in my node.js project. I am a beginner in JavaScript and have no idea where the problem is originating from. I am using express, MongoDB, and mongoose. The problem se ...

Discover the magic of using jQuery's .map() method with

$(function() { $('input[type=checkbox]:checked').map(function() { alert($("#chk_option").val()); $("#chk_option").val(this.value); }).get(); }); HTML code <div> <center> <form id="form_tarif" class="form-horizo ...

Showing a Div after a designated time of page loading: A simple guide

While I have a good understanding of CSS, Javascript is still new territory for me. I could use some guidance on the following task. My goal is to display a fixed div at the bottom of the screen, but I only want it to appear after a specific delay, say 10 ...

Angular 5 with Typescript encountered a failure in webpack due to the absence of the property "data" on the Response

I am encountering an issue during webpack compilation. It compiles successfully if I remove .data, but then the page crashes with calls from template->component (which in turn calls a service). Here is the error I am facing: ERROR in src/app/compone ...

Generate an array in JavaScript using the values from input fields

Is there a way to create a JavaScript array that stores the values of all input fields with the class 'agency_field', when there are x number of these fields in the form? I attempted to achieve this using jQuery, but encountered a syntax error. ...

"Error: Unable to Access Map Property of Undefined - Troubleshooting Twitter API Integration in

Hello everyone! I am still new to React, so please be patient with me :0) Currently, I am working on developing a simple React application where users can input a search term and receive a list of Tweets. I am in the initial stages of building this app us ...

Encountering difficulty when determining the total cost in the shopping cart

I am currently working on a basic shopping cart application and I am facing an issue when users add multiple quantities of the same product. The total is not being calculated correctly. Here is my current logic: Data structure for Products, product = { ...

Discover the process of incorporating secondary links into your dabeng organizational chart!

I need to incorporate dotted lines on this chart, such as connecting leaf level nodes with middle level nodes. import OrgChart from '../js/orgchart.min.js'; document.addEventListener('DOMContentLoaded', function () { Mock.mock(&apo ...

Continue running the ajax request repeatedly until it successfully retrieves results

At the moment, I am using a basic ajax call to retrieve data from our query service api. Unfortunately, this api is not very reliable and sometimes returns an empty result set. That's why I want to keep retrying the ajax call until there are results ( ...

Creating an HTML table from JSON data using JavaScript

I recently wrote some code that reads the contents of an XML file and converts it into JSON. The JSON data is then displayed in an HTML table. Everything seems to be working fine, but there's one issue - the first row of the table always shows as "und ...

Dealing with textarea in Javascript

I am new to JavaScript and facing a challenge in creating a delimited string from a textarea input. The issue is that when the textarea is passed in, it includes newlines for each row. I aim to parse the entire textarea content into a string with a delimit ...

JavaScript code does not run when a page is being included

On my AngularJS-based page, I have included some additional HTML pages like this: <div data-ng-include src="includehtml"></div> Here is the JavaScript code: $scope.selectImage = function(id) {$scope.includehtml = id;} (I pass the HTML file ...

Can a href from a "<Link>" component be passed through a Higher Order Component (HOC) into an "<a>" tag?

I am currently facing a situation with the main component where I have the following code: <Link href={'test'}> <PrimaryAnchor>Welcome</PrimaryAnchor> </Link> Within the PrimaryAnchor component, the code looks like ...

Displaying content generated by Html.Raw in MVC4 using JavaScript and AJAX is causing displacement of content within the <a> tags in the HTML

My project includes a feature where emails sent to a specific address are parsed, with attachments saved to the network and metadata stored in a database. If the serial number of a finished good matches the one included in the email, a note is generated an ...

What is the best way to execute a function once all asynchronous functions within a loop have finished?

Currently, I am facing an issue with my NodeJS forEach loop where I iterate over a series of keys and then asynchronously retrieve their values from Redis. The problem arises because the data retrieval is asynchronous, causing my array to not be fully popu ...

The `Ext.create` function yields a constructor rather than an object as

Ext.application({ name: 'example', launch: function() { var panel = Ext.create('Ext.panel.Panel', { id:'myPanel', renderTo: Ext.getBody(), width: 400, ...

Obtain a string of characters from different words

I have been trying to come up with a unique code based on the input provided. Input = "ABC DEF GHI" The generated code would look like, "ADG" (first letter of each word) and if that is taken, then "ABDG" (first two letters o ...

What does Tumblr's API primarily center on and how can you obtain an API key?

Currently, I am delving into the realm of Tumblr's API (v2). However, as a beginner, I find the documentation to be rather confusing. My current approach involves creating simple and public JavaScript scripts that specifically target frontend operatio ...

Using jQuery to loop through a table and retrieve the button value from every row

I have a challenge with dynamically created buttons in a table. My goal is to loop through the table, identify the rows that have a checked checkbox, and retrieve the value of a button within that row. I then need to store these values in an array. Unfortu ...

An essential component of Chai assertion is the inclusion of "or"

While attempting to iterate through a list of elements, I am looking to verify that the body contains either of two values. However, the current assertion method only allows me to check for one value: expect(cellText).includes(toDateFormat); Is there a wa ...