Converting a curl command into an AJAX request

Here's the curl bash command that I'm currently using:

curl -d "username=UID&password=PASS" http://localhost:8080

I'm looking to convert this into an ajax request in Java Script. Could you guide me on how to do that?

This is what I have tried so far:

$.ajax({
    url: 'http://localhost:8080',
    type: 'POST',
    data: 'username=' + form.username.value + '&password=PASS',
    success: function (data) {
        console.log(data);
    },
    error: function (data) {
        console.log(data);
    }
});

In this case, form.username.value contains UID, but it doesn't seem to be working as expected. It appears that the server isn't receiving the request. Any insights on this issue would be greatly appreciated.

Answer №1

Give this a shot:

$.ajax({
    url: 'http://localhost:8080',
    type: 'POST',
    data: {
        username: form.username.value,
        password: PASS
    },
    success: function (data) {
        console.log(data);
    },
    error: function (data) {
        console.log(data);
    }
});

Answer №2

To resolve any security issues with JavaScript being blocked, consider viewing the html file through a server rather than directly opening it as

file:///Users/user/Desktop/login.html
. I encountered similar problems with file access, but once I launched a server and accessed the page via http://localhost:8080/..., the issue disappeared.

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 is the best method to trigger a form submission using Jquery?

Happy New Year! Wishing you a joyful 2015! I have a basic PHP contact form that I'm validating with Parsley.js. The validation is working well, but I'm receiving a high volume of spam emails. I think that if I make the form submission dependent ...

TopoJSON conversion leading to surprising shapes and figures not found in original JSON

I am trying to showcase the topoJSON on a leaflet map using d3. To achieve this, I am following an example on GitHub available at this link. Here is the code I have extracted from the GitHub example: <!DOCTYPE html> <html lang="en"> <head ...

Display the div element only when the mouse is hovering over the button

I need a hidden text inside a div that will only appear when the mouse pointer is hovering over a specific button. Here is my current code: // Show help-text on hover $("#help-container") .mouseover(function(e) { $(this).find("#help-t ...

Navigating with firebase authentication and angular routing

Currently, I am in the process of developing an ionic app using version 4. For this project, I have decided to utilize firestore as my database and implement firebase-authentication for user login and registration functionalities. However, I have encounter ...

Having trouble with jQuery events not triggering properly after dynamically inserting elements using an ajax request?

It's strange that all my jQuery events become unresponsive after an AJAX call. When I use a load function, once the JSP reloads, none of the events seem to work properly. Any suggestions? Below is the code that triggers the function call: $('#p ...

Node.js Application with Role-Based Login

I am currently working on implementing role-based administration. When a user is created, the database stores a "1" for Admin or a "2" for a normal user. I want to retrieve this information from the database and display the corresponding start page based o ...

I'm experiencing some difficulties utilizing the return value from a function in Typescript

I am looking for a way to iterate through an array to check if a node has child nodes and whether it is compatible with the user's role. My initial idea was to use "for (let entry of someArray)" to access each node value in the array. However, the "s ...

Looking for guidance on optimizing the flow of my AJAX form within the MVC framework

Seeking guidance on implementing the AJAX functionality for my application. I have a button that triggers a modal dialog when clicked. The dialog contains an Ajax form with a text box and a Save button: @using( Ajax.BeginForm("SaveText", new AjaxOptions{ ...

Is it possible to center-align the text in a Material-ui TextField and enforce a minimum numerical value at the same time?

I'm facing an issue with trying to center align the text inside the TextField and also set a minimum value of 0. It seems like I can only achieve one or the other, but not both simultaneously. I referred to the material-ui page on TextField for help, ...

Looking for a way to make one image disappear briefly while transitioning to another image in Javascript

**Hey there, coding enthusiasts! I could really use some help right now. I am trying to create a smooth image transition effect using Javascript, where one image fades out while the next one fades in seamlessly. As someone who is still relatively new to pr ...

Utilize the Material UI SelectField component as a reference for handling onChange events

I'm facing a challenge with my form that contains over 15 SelectField components. I want to avoid creating multiple change handler functions, but I can't figure out how to identify the specific select field that triggered the change event in orde ...

Failing to retrieve data from Ajax response

When handling requests in a servlet, the following code snippet processes the request received: Gson gson = new Gson(); JsonObject myObj = new JsonObject(); LoginBean loginInfo = getInfo(userId,userPwd); JsonElement loginObj = gson.toJsonTree(loginInfo) ...

The Ajax request was a success, but I am unable to retrieve radio button values from the $_POST variable

My website operates fully asynchronously, generating and destroying HTML elements on button clicks that prevent navigation. In this section, I am creating a form with an array of radio boxes for rating from 1 to 10. This form is submitted using jQuery.aja ...

Display data when clicking on Tailwind

I am currently displaying a sub menu on hover using Tailwind CSS. However, I am wondering how I can achieve the exact same functionality by triggering an onclick event instead of hovering over the menu. Here is a DEMO showcasing the current setup. CODE: ...

Verify that the password is entered correctly in Angular2

My Angular2 form looks like this: this.registerForm = formBuilder.group({ 'name': ['', Validators.required], 'email': ['', Validators.compose([Validators.pattern("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+&bso ...

Adding double quotes to arrays in D3.js after using the .map method

Here is the code snippet I am working with: d3.csv("static/data/river.csv", function(data) { var latlongs = data.map(function(d) { return [d.Lat,d.Lng]; }) var lineArray1 = latlongs; console.log(lineArray1); When I view the outpu ...

Adding custom fields to the user model in MongoDB using Next Auth during login is not possible

When a user logs in via next auth, I am looking to include custom fields to the default user model. I followed the instructions provided in the official documentation at https://next-auth.js.org/tutorials/typeorm-custom-models. Here is the code snippet: ...

Deleting data from a database table using jQuery file upload

I've encountered an issue with the blueimp file upload program. The problem arises when trying to update the database table after a file has been deleted. In a scenario where two different users upload files with the same name, the records in the MyS ...

Step by step guide on transferring textbox value to hidden field

Within my GridView, there is a button at the bottom that allows users to add notes. Upon clicking this button, a pop-up window appears for users to input their note. It is essential for the system to retain the text of the note so that when the pop-up clos ...

Utilize v-for to dynamically showcase a variety of images on your

My goal is to showcase several JPG pictures located in the src/assets/images folder by utilizing the v-for directive. However, it seems like the browser is having trouble locating them. Interestingly, manually adding the pictures with the same paths works ...