Cory Rylan

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

Follow @coryrylan
JavaScript

Javascript JQuery Ajax

Cory Rylan

-

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

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

Related Posts

JavaScript

Use JavaScript Date Objects with the HTML5 Date Picker

Learn how to easily use both the native HTML5 datepicker and JavaScript Date objects together.

Read Article
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