I used this when I first created my app.
First, you need both the app web url and the host web url:
var hostweburl = decodeURIComponent( getQueryStringParameter("SPHostUrl") );
var appweburl = decodeURIComponent( getQueryStringParameter("SPAppWebUrl") );
Then you need to load the SP.RequestExecutor.JS file, which I did differently than what was in the link:
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js",
function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); }
);
}
);
execCrossDomainRequest is the function in which I executed the cross domain request. In my function, I was retrieving the five most recent items from a list called "Communication Log":
function execCrossDomainRequest() {
var context;
var factory;
var appContextSite;
context = new SP.ClientContext(appweburl);
factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostweburl);
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync(
{
url:
appweburl +
"/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Communication%20Log')/items?$select=Title,Date1&$orderby=Date1 desc&$top=5&@target='" +
encodeURIComponent(hostweburl) + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: successHandler,
error: errorHandler
}
);
}
I'm sure you can piece the rest together. This was done when I was fairly fresh with both SharePoint apps and javascript, so it may be improved.
SP.RequestExecutor("/");or thecreateitem.executeAsync({...? – wjervis Apr 18 '14 at 14:03SP.RequestExecutor. My REST url is quite different as well:appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Communication%20Log')/items?$select=Title,Date1&$orderby=Date1 desc&$top=5&@target='" + encodeURIComponent(hostweburl) + "'"hostweburl being the url for the sharepoint online site, appweburl being the app's url. – wjervis Apr 18 '14 at 14:06