Below is the way to use. Note its working in both Single Page App and Client App Part as well
var formDigest; //Declare the variable
//Document Ready
$(document).ready(function () {
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
scriptbase = hostweburl + '/_layouts/15/';
CharacterAnimation();
$.getScript(scriptbase + 'SP.Runtime.js', function ()
{
$.getScript(scriptbase + 'SP.js', function ()
{
$.getScript(scriptbase + 'SP.RequestExecutor.js', getFormDigest);
});
});
});
$(document).ready(function () {
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
scriptbase = hostweburl + '/_layouts/15/';
CharacterAnimation();
$.getScript(scriptbase + 'SP.Runtime.js', function ()
{
$.getScript(scriptbase + 'SP.js', function ()
{
$.getScript(scriptbase + 'SP.RequestExecutor.js', getFormDigest);
});
});
});
//Get Form Digest Value
function getFormDigest() {
var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
$.ajax({
url: appweburl + "/_api/contextinfo",
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "text/xml"
},
success: function (data) {
requestdigest = data;
var formDigest = data.d.GetContextWebInformation.FormDigestValue;
DoSomething(formDigest);
},
error: function (err) {
alert(JSON.stringify(err));
}
});
}
function getFormDigest() {
var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
$.ajax({
url: appweburl + "/_api/contextinfo",
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"contentType": "text/xml"
},
success: function (data) {
requestdigest = data;
var formDigest = data.d.GetContextWebInformation.FormDigestValue;
DoSomething(formDigest);
},
error: function (err) {
alert(JSON.stringify(err));
}
});
}
//Do Something Method
function DoSomething(formDigest) {
var urltest = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('List1')/getitems(<Query><Where><BeginsWith><FieldRef">query=@v1)?@v1={\"ViewXml\":\"<View><Query><Where><BeginsWith><FieldRef Name='Title'/><Value Type='Text'>A</Value></BeginsWith></Where></Query><RowLimit>1</RowLimit></View>\"}&@target='" + hostweburl + "'";
$.ajax({
url: urltest,
type: "POST",
headers: {
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose",
"X-RequestDigest": formDigest
},
contentType: 'application/json',
success: function (data) {
alert(data); // Finally found [Object][Object] :)
},
error: function (data) {
alert(data.responseText);
}
});
}
function DoSomething(formDigest) {
var urltest = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('List1')/getitems(<Query><Where><BeginsWith><FieldRef">query=@v1)?@v1={\"ViewXml\":\"<View><Query><Where><BeginsWith><FieldRef Name='Title'/><Value Type='Text'>A</Value></BeginsWith></Where></Query><RowLimit>1</RowLimit></View>\"}&@target='" + hostweburl + "'";
$.ajax({
url: urltest,
type: "POST",
headers: {
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose",
"X-RequestDigest": formDigest
},
contentType: 'application/json',
success: function (data) {
alert(data); // Finally found [Object][Object] :)
},
error: function (data) {
alert(data.responseText);
}
});
}