I am combining a MVC 5 SharePoint Provider Hosted App together with pages with just javascript. I created a page with the following javascript:
<script type="text/javascript">
var hostweburl;
var appweburl;
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
// resources are in URLs in the form:
// web_url/_layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js files and continue to the successHandler
// Load the js files and continue to the successHandler
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js",
function () { $.getScript(scriptbase + "SP.RequestExecutor.js", InitForm); }
);
}
);
});
function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
// prepare the data
function InitForm() {
var json;
// executor: The RequestExecutor object
// Initialize the RequestExecutor with the app web URL.
var executor = new SP.RequestExecutor(appweburl);
var completeUrl = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('Ploegen')/items?@target='" + hostweburl + "'";
var testSource = executor.executeAsync({
url: completeUrl,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
error: function (data) {
alert("error: " + data.statusText);
},
success: function (data) {
alert("succes: " + data.d.results);
json = data.d.results
}
}).responseText;
}
</script>
The only problem is that when I run the script I got the message the name 'target' does not exist in the current context.
HostWebUrl and WebAppUrl are filled, but still getting @target errors, even when I am only creating a string with @target init.
What am I doing wrong, because I don't get it?
Thanks