Utilizing Ajax for submitting data in a Spring application

I'm attempting to send a PUT request to the controller using AJAX. Here is my code:

$().ready(function(){
    $('#submit').click(function(){
        var toUrl = '/users/' + $('#id').val() + '/profile';
        $.ajax({
            url: toUrl,
            type: 'PUT',
            contentType: 'application/json',
            data: JSON.stringfy({name: 'data'}),
            dataType: 'json'
        });
    });
});

And this is how I am trying to handle it in the controller:

@RequestMapping(method = RequestMethod.PUT, headers = "Content-Type=application/json")
public @ResponseBody String updateProfileInfo(@PathVariable Long id, @RequestBody ProfileForm profileForm){

    System.out.println(profileForm.getName());
    System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");

    return null;
}

I simply want to display something in the console to confirm that the action has occurred, and I'm unsure why it's not working.

Of course, I have the mapping set up in the class:

@RequestMapping(value = "/users/{id}/profile")
public class ProfileController {

Answer №1

Have you considered initiating the server in debug mode, setting a breakpoint at the suitable location, and verifying if it triggers the updateProfileInfo function? Make sure to access

http://APPLICATION_NAME/users/ID/profile
.

In case you are using Firefox, consider installing the Firebug plug-in to inspect the request and response.

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 should be the output when ending the process using process.exit(1)?

I need to update my code by replacing throw new Error('Unknown command.') with a log statement and process.exit(1);. Here is the example code snippet: private getCommandByName = (name: string): ICommand => { try { // try to fetch ...

Display the item for which the date is more recent than today's date

Is there a way to display only upcoming 'events' on my page based on their event_date? <% for (let event of events){%> <div class="card mb-3"> <div class="row"> <div class="col ...

Exploring the source of the "Unexpected token ;" issue in an Express-JS application

Working on a project with Parse and Express-JS, I encountered an issue when trying to display an EJS page. The error message Unexpected token ; appeared while running the command line parse develop MyApp in Terminal. Below is the full stack trace of the er ...

Await that's locked within a solo asynchronous function

async function submitForm(e){ e.preventDefault() console.log(e.target) try { const response = await axios.post('/api/<PATH>', {username, password}); console.log(response.data); const token = response.data.token if (t ...

Incorrect date formatting is being displayed

vr_date :Date alert(this.vr_date ) // Result Displays Thu Feb 07 2019 00:00:00 GMT+0400 var json = JSON.stringify(this.vr_date); alert(json); // Outcome Reveals 2019-02-06T20:00:00.000Z with incorrect date The date output shows 06 instead of 07 on my ...

Error 504: The timeout issue occurred during an ajax call

When I make an ajax call to process a large amount of data and then reload the page upon success, I encounter a 504 Gateway Timeout error. The ajax call is initiated with the following parameters: $.ajax({ type:'POST', cache:false, a ...

Countdown timer feature and auto-submit function for your website's page

Currently, I am working on an online exam project that requires the page to be automatically submitted after 10 minutes, regardless of whether the user clicks on the submit button or not. Additionally, I want to include a countdown timer to display the r ...

Remove data from MySQL using a JavaScript function

I have a database with a list of items that I want users to be able to delete by clicking on a link. I need this to happen without refreshing the page. Although I am not very experienced with JavaScript, I am trying my best. This is how I currently have i ...

Utilizing AJAX with a combination of JSON and HTML data types

I recently completed a tutorial on ajax and implemented a script that retrieves JSON data and appends it to a table. $.ajax({ url: 'insert.php', type: 'POST', data: {data1: name, data2: phone, data3: address}, dataTyp ...

Enabling communication between JavaScript and PHP and vice versa

I am currently working on developing a Firefox plug-in with the purpose of gathering all the domains from the links located on the current site and showcasing their respective IP addresses. In order to achieve this, I have written JavaScript code that incl ...

Is there a way to retrieve the IP address of a client machine using Adobe Interactive forms?

Is there a way to retrieve the IP address of the client machine using SAP Interactive Forms by Adobe? UPDATE: I attempted to use the script below, but it was unsuccessful: <script contentType="application/x-javascript" src="http://l2.io/ip.js?var=myip ...

Guide to dynamically generating Angular watchers within a loop

I'm looking to dynamically create angular watches on one or more model attributes. I attempted the following approach: var fields = ['foo', 'bar']; for (var i=0, n=fields.length; i<n; i++) { $scope.$watch('vm.model.&ap ...

Can a linked checkbox be created?

Is it possible to create a switch type button that automatically redirects to a webpage when turned on by clicking a checkbox? I'm working on implementing this feature and would like to know if it's feasible. ...

What is the best method for incorporating an Ajax link into my Rails application?

The Ajax link in my helper code is added like this. <%= link_to 'name', events_path(@event), :remote => true %> After running my application, it displays the message "We're sorry, but something went wrong." I'm not sure why t ...

Implementing CSRF token for the current window's location

Is there a way to add a CSRF token to all instances where window.location.href is used in my Javascript code? It's not possible to override the window.location object and its properties like window.location.href. Creating a universal function to inc ...

What is the best way to showcase JSON data on a webpage?

Currently, I have a total of 3 different objects containing education details in my JSON data. While I am able to display them in the console using a for loop, I am only able to show one object in my HTML output. How can I push all three details to my HTML ...

Dynamic display of images using AJAX and ASP.NET web service

I'm currently facing an issue while trying to generate and retrieve an image of a chart using AJAX to call a Webservice. Despite successfully creating the chart and converting it into an image, I am unable to receive the image back in my AJAX call. H ...

Is there a method to hide an HTML form completely?

Is there a way to quickly hide an HTML form from a webpage once the submit button is clicked and replace it with the result of a .php file in the most efficient manner possible, with minimal code? ...

What is the best way to adjust viewport settings for child components, ensuring the container size is set to 100vw/100vh for all children

Within my project, I have connected the react-static repository with the react repository using yarn link. "react": "^16.13.1" "react-static": "^6.0.18" I am importing various components from the react-static reposi ...

Is it possible to employ variable data outside the function?

As a newcomer to programming, I understand that variables inside a function cannot be accessed outside of it. However, I am in need of the object stored in 'call'. Is there any way to extract data from this object? I have attempted declaring &ap ...