Cory Rylan

My name is , Software Developer and Speaker. Building Design Systems and Web Components.

Follow @coryrylan
JavaScript

Javascript JQuery Ajax

By 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
    }
  });
}
Bluesky Facebook LinkedIn Email
 

Related Posts

JavaScript

JavaScript ES6 let

Learn about how the ES6/ES2016 let variable statement works in JavaScript and TypeScript.

Read Article
JavaScript

JavaScript Prototypal Inheritance

A beginner tutorial on understanding JavaScript prototypal inheritance.

Read Article
JavaScript

JavaScript ES6 Class Syntax

A beginner overview tutorial of the ES6/ES2015 JavaScript Class syntax.

Read Article