javascript ReferenceError: _spPageContextInfo 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28828286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReferenceError: _spPageContextInfo is not defined
提问by Ruchi
I have the following code in my js file:
我的 js 文件中有以下代码:
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
但是我收到以下错误:ReferenceError: _spPageContextInfo is not defined 我不知道我错在哪里以及如何消除这个错误
回答by vbilenko
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.
代码是正确的。但是不要在页面加载之前尝试运行它。_spPageContextInfo 在正文部分的某处定义。所以,它在头部部分不可用。
Just try to run your code on DOM ready, or put it at the end of the page.
试着在 DOM 上运行你的代码,或者把它放在页面的末尾。
回答by Murtuza Husain
Since its a sharepoint code try running this:
由于它是共享点代码,请尝试运行:
SP.SOD.executeFunc("SP.js","ClientContext",functionName)
This ensures that the specified file (sp.js) that contains the specified function (ClientContext) is loaded and then runs the specified callback function name.
这确保加载包含指定函数 (ClientContext) 的指定文件 (sp.js),然后运行指定的回调函数名称。
Realize that SharePoints _spPageContextInfo
is being used before it gets auto initialized
SharePoints _spPageContextInfo
在自动初始化之前意识到正在使用