0

I am getting the message '$' is undefined below is my code these are the script tags i am using


  script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" /script
script type="text/javascript"

$(document).ready(function () {
    // Wait until SP.JS has loaded before calling getWebUserData 
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
});

var context = null; 
var web = null; 
var currentUser = null;
var userGroups = null;
var displayGroups = null;

function getWebUserData() {

    context = new SP.ClientContext.get_current();   // Get the current user 
    web = context.get_web();                        // Get the current web    
    currentUser = web.get_currentUser();            // Get the current user 
    context.load(currentUser);                      // Load the current user 

    context.executeQueryAsync(onSuccessMethod, onFailureMethod);
}

function onSuccessMethod() {

    alert('User name:' + currentUser.get_title() + '\n Login Name:' + currentUser.get_loginName());

}

function onFailureMethod(sender, args) {

    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

}
/script

  • 2
    I assume you are just missing the < > around the script tags? A cut and paste error? Also I recommend using a protocol agnostic src="//ajax..." – Rothrock Dec 2 '16 at 19:45
2

'$' is undefined is mean the jquery is not loaded, so try to make sure the code format like the following (I tried this on my side in script editor that have worked properly)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
    // Wait until SP.JS has loaded before calling getWebUserData 
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
});

var context = null; 
var web = null; 
var currentUser = null;
var userGroups = null;
var displayGroups = null;

function getWebUserData() {

    context = new SP.ClientContext.get_current();   // Get the current user 
    web = context.get_web();                        // Get the current web    
    currentUser = web.get_currentUser();            // Get the current user 
    context.load(currentUser);                      // Load the current user 

    context.executeQueryAsync(onSuccessMethod, onFailureMethod);
}

function onSuccessMethod() {

    alert('User name:' + currentUser.get_title() + '\n Login Name:' + currentUser.get_loginName());

}

function onFailureMethod(sender, args) {

    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

}
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.