javascriptajaxangularjssharepoint-2013

ReferenceError: _spPageContextInfo is not defined


I have the following code in my js file:

var configNews = {
    url:_spPageContextInfo.webAbsoluteUrl,
    newsLibrary: 'DEMONews',
    listId: ''
};

// Getting the ListID
$.ajax({
    url: configNews.url + "/_api/web/lists/getbytitle('" + configNews.newsLibrary + "')?$select=Id",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        // Returning the results
        configNews.listId = data.d.Id;
    },
    error: function (data) {
        alert(data);
    }
});

But I am getting the following error: ReferenceError: _spPageContextInfo is not defined I don't know where I am wrong and how to remove this error


Solution

  • The code is correct. But don't try to run it before page loaded. _spPageContextInfo defined somewhere in body section. So, it`s not available in head section.

    Just try to run your code on DOM ready, or put it at the end of the page.