JavaScript

Javascript JQuery Ajax

Cory Rylan

- 1 minute

Updated

This following example is a small snippet of code for a AJAX web service call to a .NET service with JQuery. We will be sending a email and name to our web service to sign up for a email subscription.

/*JS Email Subscription AJAX web service*/

callService();

function callService() {
// Here we will create our data object to send. In this example we are
// sending a email subscription request. We will send a email address and a name.
var data = {
email: $('#emailTextBox').val(),
name: $('#nameTextBox').val()
};

$.ajax({
type: 'POST',
url: '/myWebServices.asmx/emailSubscription',
data: JSON.stringify({ data: data }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(response) {
if (response.d == true) {
alert('You have been subscribed');
//Do any work you want if the web service was successful.
} else {
alert('Error');
//Or throw a error
}
},
error: function(error) {
alert('error');
//Or throw a error if service failed
}
});
}
 

No spam. Short occasional updates on Web Development articles, videos, and new courses in your inbox.

Related Posts

Web Performance

Design System Performance with Clarity Core Web Components

Learn how to build high performance UI and Design Systems on the Web using Clarity Core.

Read Article
Web Components

State of Web Components in 2020

Learn a brief overview on Web Components and the latest tech available to build and distribute components across the Web.

Read Article
JavaScript

Wrapping DOM Text Nodes with JavaScript

Sometimes, due to CSS constraints, we need to find DOM text nodes and wrap them with a span or div. This post, we will see how to preserve any existing references safely.

Read Article