How to pass JSON data to server-side using jquery Ajax

0 votes
With the help of example and code, can you explain how to pass JSON data to server-side using jQuery Ajax?
Dec 19, 2024 in Java-Script by Ashutosh
• 18,160 points
66 views

1 answer to this question.

0 votes

You can use jQuery's $.ajax() method:

Prepare the Data: Create a JavaScript object containing the data you wish to send.

var dataToSend = {

    username: 'exampleUser',

    password: 'examplePass'

};

Convert to JSON: Use JSON.stringify() to convert the object into a JSON-formatted string.

var jsonString = JSON.stringify(dataToSend);

Configure the AJAX Request: Set up the $.ajax() method with the appropriate options:

$.ajax({

    type: 'POST',

    url: 'https://example.com/api/endpoint',

    contentType: 'application/json',

    data: jsonString,

    dataType: 'json',

    success: function(response) {

        console.log('Success:', response);

    },

    error: function(xhr, status, error) {

        console.error('Error:', error);

    }

});
answered Dec 24, 2024 by Navya

Related Questions In Java-Script

0 votes
1 answer

How to send data in request body with a GET when using jQuery $.ajax()?

Hello @kartik, Sending the data in your scenario,I ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,840 points
19,949 views
0 votes
1 answer

How to Abort Ajax requests using jQuery?

Hello kartik, Just use ajax.abort(). For example you could ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
9,732 views
0 votes
3 answers
0 votes
1 answer

How to trigger jquery.ajax() error callback based on server response, not HTTP 500?

Hello @kartik, The error callback will be executed ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,840 points
17,826 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
16,358 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,704 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
3,483 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
5,294 views
0 votes
1 answer

How to sum the values of a javascript object

Certainly, I'd be happy to help you ...READ MORE

answered Sep 25, 2023 in Java-Script by Edureka
• 12,690 points
4,801 views
0 votes
1 answer

How to create custom pagination in JavaScript?

It enhances user experience by dividing large ...READ MORE

answered Dec 24, 2024 in Java-Script by Navya
76 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP