I used the code from "How to: Complete basic operations using JavaScript library code in SharePoint 2013", the part underneath "SharePoint website tasks".
I'll just paste the code here, so that you can see it without having to open the above site:
function retrieveWebSite(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
this.oWebsite = clientContext.get_web();
clientContext.load(this.oWebsite);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
alert('Title: ' + this.oWebsite.get_title() +
' Description: ' + this.oWebsite.get_description());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\nStackTrace: ' + args.get_stackTrace());
}
Whenever I run it, I get the following error:
Request failed. Unexpected response from server. StackTrace: null
I am absolutely clueless on why it won't work. I even tried adding the code below at the top of the script (which I collected from browsing other questions here), in the hopes that it would help. But alas. Even that won't do it.
SP.SOD.executeOrDelayUntilScriptLoaded('SP.UserProfiles.js', "~sitecollection/Style%20Library/Scripts/jquery.SPServices-2013.01.min.js");
SP.SOD.executeFunc('SP.js', 'SP.ClientContext');
$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(retrieveWebSite, "sp.js"); });
Any help would be greatly appreciated.
EDIT:
This is the code I use to loop through all elements in the quick launch navigation (it is a term driven navigation), as soon as it encounters an element that has a href attribute ending with "Virtual-desk.aspx", it launches the function retrieveWebsiteUrl(). So basically with this code I give a value to the siteUrl variable. The function runMe() is called as soon as the document is ready.
$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(runMe, "sp.js"); });
var siteUrl = '';
var counter = 0;
var clientContext;
function runMe() {
var $this = $("#NavRootAspMenu");
if($this != null) {
$this.find('li').each(function(i){
counter = counter + 1;
siteUrl = $this.find("a.static")[i].href;
console.log('List item found, count = ' + counter + '; ---------- ' + siteUrl);
if (siteUrl.indexOf('Virtual-desk.aspx') > -1) {
console.log('Encountered the page "Virtual-desk.aspx". Now running retrieveWebSite().');
retrieveWebSite(siteUrl);
}
});
}
}