JQuery WebAPI call

by Luis Valle

HTML

<script src="https://code.jquery.com/jquery-1.12.3.js"></script>
<div>
  <input id="txtAPIKey" type="text" placeholder="Enter APIKey" />
</div>
<div>
  <button id="btnFindSalesInformation">Find Sale Information</button>
  <button id="btnLookupSerialNumberAttributes">Lookup SerialNo Attributes</button>
</div>
<div id="dvInput">

</div>

JavaScript

$("#btnLookupSerialNumberAttributes").on("click", function () {
	if ($.trim($("#txtAPIKey").val()) == "") {
  	$('#dvInput').append('<div>No APIKey Specified!</div>');
    return;
  }
  
	AJAXcall("LookupSerialNumberAttributes", {
  	SerialNumber: "SHA00123"
  })
  .then(function (response) {
  	$('#dvInput').append('<div>' + JSON.stringify(response) + '</div>');
  })
});

$("#btnFindSalesInformation").on("click", function () {
	if ($.trim($("#txtAPIKey").val()) == "") {
  	$('#dvInput').append('<div>No APIKey Specified!</div>');
    return;
  }
  
	AJAXcall("FindSalesmanInformationForClaim", {
  	DealerCode: "DS026",
  	SerialNumber: "LAY00711",
  	TransactionDate: "2018-11-15T16:03:31.518Z"
  })
  .then(function (response) {
  	$('#dvInput').append('<div>' + JSON.stringify(response) + '</div>');
  })
});

function AJAXcall(method, spParams) {
  var d = $.Deferred();

  $.ajax({
    url: "http://localhost:21628/api/UEMData/" + method, //http://localhost:21628/api/CatRewards/     https://testdata.fastiron.com/api/CatRewards/
    data: JSON.stringify(spParams),
    headers: {
        APIKey: $.trim($("#txtAPIKey").val())
    },
    dataType: 'json',
    type: 'POST',
    contentType: 'application/json'
  })
    .done(function (response) {
    d.resolve(response);
  })
    .fail(function (jqXHR, textStatus, errorThrown) {
    errorThrown = (errorThrown != '' ? 'Error Thrown: "' + errorThrown + '"' : null);
    d.resolve({ error: true, errorMsg: errorThrown });
  });

  return d.promise();
}