SyntaxError Unexpected token in JSON at position 0

Hello everyone,

Today I will talk about a problem concerning the use of PNP JS Core on an environment 2013 OnPremise.

Problematic

Like many I started using PNP JS Core for my developments. After retrieving some information and doing some conclusive tests on an Office 365 environment, I wanted to reuse my code on a SharePoint 2013 OnPremise.

And there .. Boom!

SyntaxError: Unexpected token < in JSON at position 0

The code does not have anything complicated, it will only read the contents of a list.

Reason

Looking a bit, I saw that the return of the REST call was XML and not JSON. It seems that this comes from the basic configuration of SharePoint 2013 SP1 https://github.com/SharePoint/PnP-JS-Core/issues/125

Resolution

The solution to this problem is to pass a configuration before making the call: https://github.com/SharePoint/PnP-JS-Core/pull/127

pnp.setup({
        headers: {
            "Accept": "application/json; odata=verbose"
        }
    });

Simple but when you start using PNP JS it can be useful to know.

Hoping it helps.

NB : Thanks to Yannick for helping the noobie I was in this situation !

Christopher.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s