Isaac
asked on
jquery spservices 'undefined'
I created the following function and I'm trying to figure out whey the alert(boolval) returns 'undefined'. The alert("Hello") works though.
Any ideas please?
Thanks!
function GetGroupOfUser(){
var curUser = $().SPServices.SPGetCurrentUser();
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function(xData, Status) {
if($(xData.responseXML).find("Group[Name='ACC']").length == 1) {
alert("Hello");
var boolVal = "True";
alert(boolval);
}
else
{
var boolval = "False";
alert(boolval);
}
}
}); /*close().SPServices */
}
Any ideas please?
Thanks!
ASKER
That does not work either.
I switched it around...here's the full code that's calling the function...
I switched it around...here's the full code that's calling the function...
<script type="text/javascript" language="javascript" src="http://moss2007vm1:35714/sdimetrics/js%20Assets/SDIfunctions.js"></script>
<script type="text/javascript" language="javascript" src="http://moss2007vm1:35714/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" language="javascript" src="http://moss2007vm1:35714/js/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
var valBool = GetGroupOfUser();
alert(valBool);
$().SPServices({
operation: "GetListItems",
async: false,
listName: "SDIMetrics",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='MAJCOM' /><FieldRef Name='ID' /></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
$("#tasksUL").append(liHtml);
alert(liHtml);
});
}
});
});
</script>
function GetGroupOfUser(){
var boolval = false;
var curUser = $().SPServices.SPGetCurrentUser();
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function(xData, Status) {
if($(xData.responseXML).find("Group[Name='ACC']").length == 1) {
alert("Hello");
var boolVal = "True";
return boolVal;
}
}
}); /*close().SPServices */
}
If you're trying to get the alert which is in your first scriptblock it will return undefined when the if clause is not firing.
In other words to return the boolVal you should take it out of the if clause of the GetGroupOfUser() function like so:
It is worthing to note, that if your ajax call fails then no boolVal will be returned either so you might return that value even outside your ajax call after the /*close().SPServices */ comment to return at least false if ajax fails.
In other words to return the boolVal you should take it out of the if clause of the GetGroupOfUser() function like so:
function GetGroupOfUser(){
var boolval = false;
var curUser = $().SPServices.SPGetCurrentUser();
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function(xData, Status) {
if($(xData.responseXML).find("Group[Name='ACC']").length == 1) {
alert("Hello");
var boolVal = "True";
}
return boolVal;
}
}); /*close().SPServices */
// better to put the return boolVal here, cause this returns even if ajax fails.
}
It is worthing to note, that if your ajax call fails then no boolVal will be returned either so you might return that value even outside your ajax call after the /*close().SPServices */ comment to return at least false if ajax fails.
ASKER
I made the change but now i get "boolVal" is undefined.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When I call the function from the code below, if the boolVal is false, I get boolVal is undefined. Everything is ok if it's true though.
<script type="text/javascript" language="javascript">
$(document).ready(function() {
var getBoolVal= [b]GetGroupOfUser()[/b];
if(getBoolVal)
{
alert("True");
}
else
{
alert("False");
}
$().SPServices({
operation: "GetListItems",
async: false,
listName: "SDIMetrics",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='MAJCOM' /><FieldRef Name='ID' /></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
$("#tasksUL").append(liHtml);
alert(liHtml);
});
}
});
});
</script>
Try like this
var boolval = "True";
alert(boolval);