Uncaught TypeError: $.ajax(…).then is not a function

Recently while working on Rest API with SharePoint Online, I got one script error which says “Uncaught TypeError: $.ajax(…).then is not a function”.

We were basically trying to retrieve the Office 365 SharePoint online server’s current date and time by using the below code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$().ready(function () {
getSPCurrentTime(_spPageContextInfo.webAbsoluteUrl)
.done(function (value) {
alert(value.toUTCString());
})
.fail(
function (error) {
alert(JSON.stringify(error));
});
})
function getSPCurrentTime(webUrl) {
return $.ajax({
url: webUrl + "/_api/web/RegionalSettings/TimeZone",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
}).then(function (data) {
var offset = data.d.Information.Bias / 60.0;
return new Date(new Date().getTime() - offset * 3600 * 1000);
});
}
</script>

But when we run the above code, it gave an error as: Uncaught TypeError: $.ajax(…).then is not a function

And the error looks like below:

Uncaught TypeError: $.ajax(...).then is not a function
Uncaught TypeError: $.ajax(…).then is not a function

Uncaught TypeError: $.ajax(…).then is not a function

The error was coming because of an mismatch in the jquery file I have referred. I had referred the jQuery version like below:
https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

When I change the version and the URL like below, it started working.

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

Read some SharePoint Rest API examples:

This tutorial, we learned how to fix error, Uncaught TypeError: $.ajax(…).then is not a function which comes in SharePoint Online.

>