1

I created a new page in team site and dropped script editor web part. I am using the following code to read items from a list in SharePoint 2016 but it doesn't seem to work. It does show that jquery is loaded as well as alert(siteUrl) but after that there is another alert("testing") which it doesn't display. No error is shown either.

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=" crossorigin="anonymous"></script>

<script language="javascript">
if (typeof jQuery != 'undefined')
{
    alert("jQuery library is loaded!"); //this works
}
else
{
    alert("jQuery library is not found!");
}

    var siteUrl = _spPageContextInfo.webAbsoluteUrl;
    alert(siteUrl); //this works
    $.ajax({
        url: siteUrl + "/_api/web/lists/getbytitle('MyList')/items",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data)
        {
            alert("testing"); //this doesn't work
        },
        error: function (data)
        {
            alert("Error: " + data); //this doesn't work
        }
    });
</script>

I can open page directly in browser if I type this URL:

http://myserver/sites/teamsite/_api/web/lists/getbytitle('MyList')/items

Can you please check and let me know what am I doing wrong here?

  • 1
    Ditch jQuery and learn to use Microsofts own PnPCoreJS library: github.com/SharePoint/PnP-JS-Core Maybe a longer learning path (now) but at least one with a future – Danny '365CSI' Engelman May 12 '17 at 19:50
  • 2
    @Danny'365CSI'Engelman - pnp js core, even though its highly useful is at the end of the day just another abstraction to sharepoint rest api :) Law of leaky abstraction – Gautam Sheth May 12 '17 at 20:07
  • 1
    My main point was the Ditch jQuery part...Yes, all libraries/frameworks suck (the reverse of leaking) at one (or more) points. So does that blogpost from 2002 you refer to. As the whole universe understands binary, It can be abstracted to the unleaky 101010 🙏 – Danny '365CSI' Engelman May 13 '17 at 8:30
  • 1
    @Gautam, thanks for that link, made my week, the blogpost sucks (too long a text for what we all know) but it got me to his latest project: glitch.com/about Feels a lot like Popfly, which Microsoft discontinued.... and yes, they are still tools that abstract – Danny '365CSI' Engelman May 13 '17 at 8:52
2

The issue lies with your jQuery file.

Use either of the below jquery references -

1) https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js

or

2) https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js

or

3) https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js

jQuery 2.x drops support for IE 8

Also, related to the version that you have referred, the jquery.slim.js, there are below mentioned issues.

The following functions are removed in the slim version of jquery which makes it 20% smaller.

  1. jQuery.fn.extend
  2. jquery.fn.load
  3. jquery.each // Attach a bunch of functions for handling common AJAX events
  4. jQuery.expr.filters.animated
  5. ajax settings like jQuery.ajaxSettings.xhr, jQuery.ajaxPrefilter, jQuery.ajaxSetup, jQuery.ajaxPrefilter, jQuery.ajaxTransport, jQuery.ajaxSetup
  6. xml parsing like jQuery.parseXML,
  7. animation effects like jQuery.easing, jQuery.Animation, jQuery.speed

That's most likely causing the issue of your ajax request failing. Other than that, your code looks good and should work.

Reference - difference between normal and slim package of jquery

  • 1) is native JavaScript Object.assign 3) is native JavaScript .forEach() – Danny '365CSI' Engelman May 13 '17 at 8:36

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.