"Store JSON data into a variable by formatting it through an AJAX POST request

If I want to make a controller call like this:

 name.php?data={"user":"test","pass":"test"}

To retrieve the necessary information using .ajax, I need assistance in configuring the variable to be sent in that specific format.

I previously used the following code:

 var arr = [{
     data: {
      "user" : $("#usuario").val(),
      "pass" : $("#password").val()
 }];
 arr = JSON.stringify(arr);

However, it does not produce the correct output. I've been advised to send the variable with the json included in it.

 function callAjax(url, arr) {
  var response = null;
  jQuery.ajax({
    url: url,
    type: 'POST',
    data: arr, 
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    async: false,
    success: function(data) {
   response = data;
    },
    error: function(jqXHR, textStatus, errorThrown) {
   response = errorThrown;
},
timeout: 5000
 });
 return response;
 }

Any suggestions?

Warm Regards!

Answer №2

By utilizing the .post method, I successfully resolved the problem

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

Unlocking the value of the "input" field within an EventListener function in Angular directly from the DOM

In my "characters" module, there is a form with a text field and a button. When the button is clicked, it triggers a function but I am struggling to retrieve the current input text and pass it to the async function. HTML: https://i.sstatic.net/DMF8w.png ...

What is the method to run a script within a particular div?

Here is an example of the DOM structure: <div> <button>Click me</button> <img src=#> </div> <div> <button>Click me</button> <img src=#> </div> <div> <button>Clic ...

Should we avoid using 'RedirectToAction' with AJAX POST requests in ASP.NET?

As a newcomer to jQuery, Json, and Ajax, I am putting in the effort to grasp the concepts clearly, but I am facing some difficulties. I have an ajax POST Delete method that is currently functional, however, my professor has suggested that I refactor the c ...

Unraveling a Secret Communication in a Text Document Using Javascript

I am currently working on developing a special function called decode(message_file) that has the task of reading an encoded message from a .txt file and returning the decoded version as a string. The function itself must have the ability to handle an input ...

Transforming approved material in Umbraco into serialized content

Currently, I am in the process of developing an API for a custom application that my company is working on. One aspect of this task involves extracting published content in JSON format. However, when attempting to serialize ipublishedcontent directly, it c ...

Modify the height using JavaScript and then revert back to the initial CSS height

Looking for assistance with a JavaScript issue that I'm currently facing. Q1: Javascript Function to Return Image Height and Margin My website layout consists of a horizontal scroll of smaller images arranged in a grid, each with different height pe ...

Retrieving DWR form information within a Spring Interceptor

As part of my DWR action, I have a method with the signature: String joinGroup(String groupId, String groupName); The method works well when called through a DWR AJAX request. However, I am currently working on implementing a Spring interceptor, similar ...

Utilizing nested JSON in Microsoft SQL Server

My table consists of USER_DATA (user data table) with 2 rows (essentially 2 entries). I crafted a nested JSON query like so -> SELECT CONCAT( first_name,' ', last_name) AS displayName, first_name AS givenName, last_name AS surname, ...

What is the process for importing extensions and files in Visual Studio Code?

Nodejs development utilizing (--experimental-modules) Current visual studio code intelligence import example: import config from "./config"; However, it should be imported as: import config from "./config.js"; An error is encountered without the . ...

What are the effects of using .json() with a response in ExpressJS?

While creating a server using ExpressJS, I encountered a dilemma regarding the use of .json() versus .send() for my responses. Although Express documentation states that .send() automatically converts JavaScript objects into JSON strings without needing to ...

Original state of class name is restored upon change

I'm currently working on creating a Lightbox-style effect using CSS and Javascript. The idea is to change the classname of an element (OverlayContainer) to toggle between a normal background and a darker overlay. However, I've run into an issue w ...

Creating a dynamic hyperlink variable that updates based on user input

I am attempting to create a dynamic mailto: link that changes based on user input from a text field and button click. I have successfully achieved this without using the href attribute, but I am encountering issues when trying to set it with the href attr ...

Using ReactJS and Hooks to update state after the .map() function

Trying to update the state using values from an array. Here is an example: const [state, setState] = useState({}); const test = [1, 2, 3]; test.map((item, i) => { setState({ ...state, [`item-${i}`]: item }); }); The current s ...

How can a TypeScript Angular directive utilize a function?

Recently, I have been following a unique Angular directive TypeScript pattern that I find really effective. The approach involves giving the directive its own isolated scope by creating a new controller. I encountered a scenario where I needed to invoke a ...

The ngMessages validation feature in AngularJS fails to remove error messages from the CSS styling

Can anyone help me figure out why I keep getting a red color underline error even though there is no actual error and validation is successful? https://i.sstatic.net/AESLl.png I created my own directive to validate passwords and I am using Angular materi ...

What is causing the while loop in Mongodb to repetitively insert the same document rather than cycling through the documents?

Currently, I am in the process of redesigning a MongoDB database structure where the reviews for a specific product will be integrated into the product document itself, rather than maintaining separate collections for products and reviews. I have created a ...

Initiate monitoring for child component modifications

I'm looking to disable 'changeDetection' for the parent component while enabling it for the child component. Can you provide an example of how this can be achieved? The parent component contains static data, meaning change detection is not ...

Navigating using passing data in ReactJs

The following data contains information about people: const people = [ { img: 11, name: "Ahmed", job: "developer", }, { img: 13, name: "Kazim", job: "Engineer", }, ...

Can an image be scanned pixel by pixel to extract and store each pixel's color in an array mapped by its coordinates?

Currently, I am working on a browser game where I have created a pixel map as a coordinate system. Each color on the map represents a unique terrain type with specific values that impact different aspects of the game. I'm looking for a solution using ...

Unable to figure out a method to properly synchronize a vue.js asynchronous function

I am facing an issue with my code where everything works fine if I uncomment the "return" statement in fetchData. How can I make sure that I wait for the completion of this.fetchData before populating the items array? I have tried using promises, async/awa ...