Transmitting user data through AJAX via POST method

How can I use the POST method to send this request?

lun = document.getElementById("lun").value;
lp = document.getElementById("lp").value;
url = "lun="+lun+"&lp="+lp;
xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("login").innerHTML=xmlhttp.responseText;
    }
    else{
        document.getElementById("login").innerHTML="Loading";
    }
}
xmlhttp.open("GET",'login.php?'+url,true);
xmlhttp.send();

Answer №1

The preferred HTTP request method to be utilized, options include "GET", "POST", "PUT", "DELETE", and other valid methods. Not applicable for URLs that do not use the HTTP or HTTPS protocol.

 fetch('https://example.com/api/data', {
   method: 'GET',
   headers: {
     'Content-Type': 'application/json'
   }
 })
 .then(response => response.json())
 .then(data => console.log(data))
 .catch(error => console.error(error));

Source: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

Answer №2

when creating a new post

consider using the following code snippet

xmlhttp.open("POST","login.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(url);

To see a demonstration, check out this link

Answer №3

xmlhttp.open("GET","authenticate.php",true);
xmlhttp.setRequestHeader("Content-type","application/json");
xmlhttp.send("username=user123&password=secure123");

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

What causes Javascript functions to continuously loop at an exponential rate?

Having an issue with a JavaScript/jQuery script that is attempting to simulate a Wisconsin card sorting task to guess the matching-card rule. The script is behaving strangely, starting from trial 3 and the console logs on line 21 of the fiddle show the res ...

Combining model with a string in an expression in AngularJS version 1

Can this text be transformed into an expression using ecmaScript 2015? The || operator seems to be causing issues. {{ ::$ctrl.realEstateProjectCurrentProduct.housingTax + ' €' || $ctrl.noDataMessage }} ...

Insert an array containing objects into another array nested within an object

I'm struggling to insert data into an array within an object. The array consists of objects, as seen in the code below. The main part of the code is functioning properly, it properly splits my payload. However, the main ecommObj is not displaying the ...

Automating the process of uploading a file to a Github repository using JavaScript and HTML

Recently, I've been tinkering with updating a file in my GitHub repository using code to set up an automated system for pushing changes without manual intervention. My approach involved creating a function that utilizes a GitHub access token to ' ...

How to emphasize a dataset in ChartJS stacked bar chart by hovering over the legend?

My Chart.js displays a horizontal stacked bar chart with legends corresponding to different classes. Here's a snippet (using dummy data, please ignore the random names): https://i.sstatic.net/XNTZZ.png The left labels represent users, while the legen ...

Struggling to accurately capture the values from checkboxes and dropdown selections to ensure the correct data is displayed. Assistance is needed in this

I am facing challenges in retrieving the accurate data for display from Mat-Select and Mat-Checkbox components. My goal is to capture the selected values from users and perform if-else statements to validate conditions, displaying the correct data view if ...

Adding information to a database by utilizing Jquery, Ajax, and PHP

Trying to use ajax to submit data to a database has been a challenge for me. Even with a simple code test, I can't seem to make it work. Here is the HTML/ajax code snippet: <?php include("osb.php");?> <script type = "text ...

Can the tooltip on c3 charts be modified dynamically?

Creating a c3 chart involves defining various properties, including a tooltip. Here is an example: generateData = () => { const x = randomNR(0, 100); const y = randomNR(0, 100); const together = x + y; return { data: { columns: [ ...

Creating a string from values in a multidimensional array by utilizing parent-child relationships and generating dynamic SQL queries

This data represents a dynamic array with sample information that needs to be utilized to create an SQL query. I am working with VueJs + Laravel. Below is the updated array data along with the methods: [ { "operator": "AND", "rules": [ { ...

Functions' length attribute

I'm a bit confused by the following scenario: $ node > var f = function() {}; > f['length'] = '11'; '11' > f['length'] 0 If you're not familiar with node, everything after > is my input, and t ...

Attempting to send an AJAX request using jQuery, receiving a successful response but encountering an error with the AJAX functionality

My AJAX request in jQuery is as follows: $.ajax({ url: "http://someurl.stuff.com", beforeSend: function(xhr) { xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Host",null); xhr.setRequestHeader("Access ...

Angular: Refresh mat-table with updated data array after applying filter

I have implemented a filter function in my Angular project to display only specific data in a mat-table based on the filter criteria. Within my mat-table, I am providing an array of objects to populate the table. The filtering function I have created loo ...

Button click not triggering Ajax functionality

I've been working on implementing a simple Ajax function in a JSP using JQueryUI. The goal is to pass two text fields from a form and use them to populate two separate divs. However, when I click the button, nothing seems to be happening. I even tried ...

Follow button on LinkedIn is secure with Google Chrome's Content Security Policy set to script-src report-sample

Having an issue trying to add a LinkedIn Follow button to the website. It works perfectly in Firefox, but is not functioning in Chrome. The Console displays the following error: The source list for Content Security Policy directive 'script-src&apos ...

Breaking down JavaScript arrays into smaller parts can be referred to

Our dataset consists of around 40,000 entries that failed to synchronize with an external system. The external system requires the data to be in the form of subarrays sorted by ID and created date ascending, taken from the main array itself. Each ID can ha ...

Create a div element that expands to occupy the remaining space of the screen's height

I am trying to adjust the min-height of content2 to be equal to the screen height minus the height of other divs. In the current HTML/CSS setup provided below, the resulting outcome exceeds the screen height. How can I achieve my desired effect? The foote ...

Several dropdowns causing issues with jQuery and Bootstrap functionality

Can anyone help me identify where I might be making a mistake? The issue is with my fee calculator that increments fees as the user progresses through the form. In this scenario, there is a checkbox that, when clicked, is supposed to display a div showing ...

JSON Generator's date formatting convention

One method I use to create a JSON object is through JSON-GENERATOR I prefer the date to be formatted like this: 2017-12-31 [ '{{repeat(5, 7)}}', { equityPriceList: [ { date:'{{date(new Date(1970, 0, 1), new Date(),[DD ...

Encountered a JavaScript error when trying to trigger an alert using PHP

I've incorporated fusion maps into one of my applications. In a particular example, I need to transfer values from one map to another chart, However, I encountered an issue where if the data passed is numeric, the alert message displays correctly, b ...

Organize an array based on two criteria using Angular

How do I sort first by payment and then by amount in Angular? While in C#, I can easily achieve this with array.orderBy(x => x.payment).thenby(x => x.amount) Is there a similar method or function in Angular for sorting arrays? I have explored the A ...