If you did not set master page on your SharePoint pages(example: Angualr JS based page), your will get undefined error when you access $(“#__REQUESTDIGEST”).val(). To get digest value use the below code so that this can be passed when you call SharePoint REST APIs.

var formDigest; // Global Variable

\$.ajax({
url: "Pass App Web or Host Web Url based on requirement" + "/\_api/contextinfo",
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "text/xml"
},
success: function (data) {
var requestdigest = data;
formDigest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function (err) {
console.log(JSON.stringify(err));
}
});