I am seeking a way to transform this request call from JavaScript into Ajax code

My JavaScript code includes a request function, but I believe it's outdated and I'd like to convert it into an AJAX call. Can someone assist with this update?

Here is the current function in my JavaScript file:


function loadRest() {
    const request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (this.readyState === 4) {
            let result = parseResponse(this.status, this.responseText);
            if (result != null) {
                Rest.rests = result;
                createTable();
            }
        }
    };
    request.open("GET", Rest.baseURL + "/byCompany/" + logginedCompanyId, true);
    request.send();
}

function parseResponse(status, responseText) {
    console.log(responseText);
    let responseObject = JSON.parse(responseText);
    if (status !== 200 || (responseObject.error && responseObject.error != null)) {
        alert("Error: " + responseObject.error);
        return null;
    }
    return responseObject.result;
}

Answer №1

To retrieve data, consider using $.get() in the following way:

$.get('api.baseURL', function(data){

// Perform actions with the retrieved data here

});

Answer №2

Here is the information you requested.

$('#ajax').click(function() { 
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "http://localhost:8080/restws/json/product/get",
        success: function(data){
            let response = JSON.parse(data);
            if(response != null) {
                API.data = response;
                displayData();
            }
        }
    });
});

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

Obtaining a value using the Node.js inquirer package

I'm currently working on a flashcard generator using the node.js inquirer package, but I'm struggling to capture the user's selection. When the user selects an option, I want to be able to log that choice, but right now it's just return ...

Improving a Vue.js notification component with data retrieved from a fetch request result

Struggling with updating the content inside a vuetify v-alert. There's an issue when trying to update Vue component properties within the sessionID.then() block after logging into a system and receiving a session id. Vue.component('query-status ...

Implementing a click function that toggles between adding and removing a class, keeping track of the number of clicks, and utilizing localStorage to prevent repeated clicking in the

Hi there! I'm currently working on a simple widget that features a clickable icon and a counter next to it. When the icon is clicked, it should toggle between an empty heart and a filled heart using addClass/removeClass. Additionally, each click incr ...

Ways to clear TextField status

My question is about a Textfield. In the case where the state is null but the text field value is showing in the Textfield. <TextField style={{ width: '65%'}} id="standard-search" ...

What is the best way to generate documentation for the useState function using typedoc?

In my code, I have a documented hook that returns a state along with multiple functions. While the functions are well-documented in typedoc, the status in the final documentation is simply displayed as a boolean without any description. export default func ...

Adding Angular directives to the DOM after the document has finished loading does not function properly in conjunction with ngAnimate

I've been working on developing an Angular service that can dynamically append a notification box to the DOM and display it without the need to manually add HTML code or write show/hide logic. This service, named $notify, can be used as follows: $no ...

What measures can be taken to avoid the entire page from reloading?

Within my page, there exist two containers. The first container is designated for displaying a list of items, while the second container showcases actions corresponding to each item. A feature allows me to add a new item dynamically to the first container ...

Surprising occurrence of the letter 's' in Postman when executing a Webmethod

Implementing a payment Checkout using Stripe. Utilized JavaScript Stripe handler to apply the Stripe charge on the transaction. Upon charging the customer, a token is returned. This token is then used to proceed with the actual payment. Below is the AJA ...

What is the method for assigning 'selective-input' to a form field in Angular?

I am using Angular and have a form input field that is meant to be filled with numbers only. Is there a way to prevent any characters other than numbers from being entered into the form? I want the form to behave as if only integer keys on the keyboard ar ...

Automatically injecting dependencies in Aurelia using Typescript

Recently, I started working with Typescript and Aurelia framework. Currently, I am facing an issue while trying to implement the @autoinject decorator in a VS2015 ASP.NET MVC 6 project. Below is the code snippet I am using: import {autoinject} from "aure ...

Obtain personalized results for implementing in Convase JS from a PHP server

I have a table on my WordPress site with the following output: { { [line1]=> array(3) {{'x'=>'5' , 'y'=>'8},{'x'=>'5' , 'y'=>'8},{'x'=>'5' , &apos ...

"Building a dynamic form with ReactJS, Redux Form, and Material UI - Implementing an

Currently working on a nested form framework that utilizes the redux form and material UI framework. The components have been developed up to this point - https://codesandbox.io/s/bold-sunset-uc4t5 My goal is to incorporate an autocomplete field into the ...

How can I redirect to another page when an item is clicked in AngularJS?

Here is an example of HTML code: <div class="item" data-url="link"></div> <div class="item" data-url="link"></div> <div class="item" data-url="link"></div> In jQuery, I can do the following: $('.item').click ...

What is the process for managing cookies on the server side using Node.js?

I have been struggling to access cookies on the server side and have not attempted anything yet. Is there a specific method or NPM package that can assist in setting or retrieving cookies on the server side? ...

How can I prevent links from being deleted in a UML state diagram using Jointjs?

My UML state diagram created with jointjs features interconnected states linked through lines. When the links are hovered over, a cross symbol appears, allowing users to delete the link by clicking on it. I am looking to prevent the cross symbol from sho ...

Submitting feedback using ajax and jquery

I need help figuring out how to display the posted comment under all existing comments, similar to Facebook's setup. Here is the code snippet I currently have: // Intercepting the submit event $('#CommentAddForm').submit(function() { ...

The Toggle Switch effectively removes the CSS class when set to false, but fails to reapply the class when set to true

Implementing a toggle switch using Bootstrap5 to control the visibility of grid lines. The setup includes adding a class to display grid lines when the toggle is true, and removing the class to hide the lines when the toggle is false. However, the issue ar ...

Creating a project that utilizes Bing Translate and the Node.js programming language

As I work on developing a web application that enables users to translate text using the Bing translator API, I am facing an issue. I attempted to execute a translator.js file via a script tag, but encountered a roadblock since Node.js code cannot be run t ...

"Resolving Compatibility Issues Between Bootstrap 4 and fullcalendar.js: Embedding Fullcalendar Within a Bootstrap 4 Modal

I am currently using fullcalendar.js in conjunction with Bootstrap 4 modal. Whenever I click on a bootstrap4 button, a modal appears with the fullcalendar component displayed. However, upon initial load, I encounter this issue: https://i.sstatic.net/nni ...

Refresh the location markers on Google Maps API to reflect their current positions

I'm currently in the process of learning how to utilize JavaScript with Rails, and I'm encountering some challenges when it comes to updating my markers based on my current position using AJAX. I suspect that the 'ready page:load' event ...