Problem with jQuery/SPServices

Feb 2, 2011 at 3:37 PM
Edited Feb 2, 2011 at 3:49 PM
I'm not entirely sure where the problem is. References? Syntax? Some setting somewhere in IE or SP? I can't for the life of me get anything in jQuery to run. I can get the first alert, and that's it. The 2 files are in the layouts folder (references won't save in my code block for some reason) and the code is in a CEWP for now (don't boo and hiss, I just need to see it work before I get all fancy with it). I don't have it all encapsulated with a $(document).ready because I need this before everything else, and its not modifying anything on the page, I just need the value, but I don't know if that affects anything as I'm relatively new to the jQuery thing.

So, if someone could take a look, and maybe a second set of eyes can tell me I'm doing it wrong, that would be great. I'd also like to know if there are any settings in IE, or SP, that would prevent the scripts from loading, thus making the SPServices call fail, and if there is anything else that could affect it.

<script type="text/javascript"> alert("First"); $().SPServices({ operation: "GetListItems", listName: "Filer Status List", async: false, CAMLViewFields: "<ViewFields><FieldRef Name='Modified'/></ViewFields>", CAMLRowLimit: 1, completefunc: function (xData, Status) { $(xData.responseXML).find("z:row").each(function() { var modDate = $(this).attr("ows_Modified"); $("#modDatex").append(modDate); )}; } )}; alert("Second"); </script>

Any kind of assistance would be greatly appreciated.
Aargh, where'd my formating go!? Sorry it looks so bad. I haven't posted here before, so I don't know what tags to use to make it look pretty. Code tag doesn't seem to help.
Coordinator
Feb 2, 2011 at 4:50 PM
Edited Feb 2, 2011 at 4:50 PM

FereroJ:

No worries about the CEWP. No booing. No hissing.

Looking at your script, you're missing the closing });

<script type="text/javascript">
 alert("First");
 $().SPServices({
  operation: "GetListItems",
  listName: "Filer Status List",
  async: false,
  CAMLViewFields: "<ViewFields><FieldRef Name='Modified'/></ViewFields>",
  CAMLRowLimit: 1,
  completefunc: function (xData, Status) {
   $(xData.responseXML).find("z:row").each(function() {
    var modDate = $(this).attr("ows_Modified");
    $("#modDatex").append(modDate); )};
   }
  )};
  alert("Second");
 });
</script>

Also, without wrapping this in a $(document).ready(), you run the risk of the element with the id="modDatex" not yet being there. Even though there are times when the $(document).ready() isn't required, I like to use it as a wrapper for anything that I want to heppen on page load. If nothing else, it makes the script easier to maintain.

M.

Feb 2, 2011 at 6:23 PM
Edited Feb 2, 2011 at 6:25 PM

Marc

Thanks for the reply. I'm not seeing where I'm missing a closing bracket. If I add another, it doesn't work at all, take it out, and I at least get the first alert. I see where "$().SPServices({" needs one, and ".each(function(){" needs one.

<script type="text/javascript">
 alert("First");
 $().SPServices({
  operation: "GetListItems",
  listName: "Filer Status List",
  async: false,
  CAMLViewFields: "<ViewFields><FieldRef Name='Modified'/></ViewFields>",
  CAMLRowLimit: 1,
  completefunc: function (xData, Status) {
   $(xData.responseXML).find("z:row").each(function() {
    var modDate = $(this).attr("ows_Modified");
    $("#modDatex").append(modDate); 
    )};
   }
  )};
  alert("Second");
</script>

Also, in my ref lines, src="/_layouts/jquery.js". That is correct when referencing something in the hive layouts folder, right?

Thanks for the help

Coordinator
Feb 2, 2011 at 6:33 PM

I'm sorry; when I reformatted to check the code, I missed one of your closing )};s. You're good there.

I'd add the $(document).ready() around this. Then if the alert fires, you know that the script reference for jQuery is correct.

src="/_layouts/jquery.js" is correct if the jquery.js file is in "\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS" (or 14 if you're working with SP2010).

M.

Coordinator
Feb 2, 2011 at 6:38 PM

Oh, and your selector for

$(xData.responseXML).find("[nodeName=z:row]")

 to work cross-browser.

Another thing to look at is what is being returned from the GetListItems call:

completefunc: function(xData, Status) {
  alert(xData.responseXML.xml);
  $(xData.responseXML).find("z:row").each(function() {
    var modDate = $(this).attr("ows_Modified");
    $("#modDatex").append(modDate); 
  )};
}

M.

Feb 2, 2011 at 6:49 PM
Edited Feb 2, 2011 at 6:52 PM

Yeah, that's where the files are, but I'm not getting anything after the $(document).ready(function(){. I'm really confounded as to why. The files are in the right spot, the references seem correct, and I can't find any syntax errors. I have jQuery 1.4.4, and SPServices 0.5.8.

Not sure what I'm missing. I can at least get the jQuery to work in a little test html page, where the js files are in the same directory as it, so the code looks fine, but as soon as its in CEWP, and I redirect to "/_layouts/" it doesn't work at all.

It just seems so weird. I can reference images and such in other web parts just fine. I have custom web parts that do far more than this that I've made, and I can't get a 19 line script to work. : (

Edit: I can't seem to make it show my reference typed out completely. type="text/javascript" src="/_layouts/jquery.js" and type="text/javascript" src="/_layouts/jquerySP.js"

Coordinator
Feb 2, 2011 at 6:51 PM

If you're not getting the alerts, then the references aren't right. Try opening the scripts directly in the browser until you get the URL right and then use that.

M.

Feb 2, 2011 at 7:01 PM

Ahahahaha. I swear I'm not dumb. I swear it.

PROTIP: Remember which virtual server you're on when placing files in the 12 Hive.

It wasn't until I started typing in the server name that it finally clicked. I have a dev server, with SharePoint loaded on it, and then another server with just SP/SQL for testing. Files were on dev box, not the server I was writing the scripts in. I feel so... special. /palmface

See, I just needed someone else to help me notice, lol. Sorry to take up your time, but thanks a lot for helping me see the light.

Coordinator
Feb 2, 2011 at 7:06 PM

Ok, now the booing and hissing. ;+)  Just kidding. I do stuff like that all the time.

Let me know how it goes!

M.

Feb 2, 2011 at 7:23 PM

Yeah, unfortunately I didn't see it until you mentioned browsing directly to them. I try my best to not look like a putz.

Looks good. I stumbled across SPServices (I actually believe it was a post you made on Jan's site that got me thinking.. haha) when having some issues with trying to write the soap envelopes and ajax calls with just jQuery. In hindsight, it was probably because jQuery wasn't in the right spot. I'm just relieved I didn't have some other deep rooted problem with SharePoint. Ick. Thanks again for all the replies. Have a delightful day.