为什么我无法通过 JavaScript 中的 OM 获取 ClientContext?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13714983/
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
Why I can't get the ClientContext by OM in JavaScript?
提问by JacobChan
Here is what I did in my project:
这是我在我的项目中所做的:
<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
alert("before");
//It can not work cause the LIST is in the subsite called "Service"
//var clientContext = SP.ClientContext.get_current();
var context =new SP.ClientContext("http://sp2010dev1:88/Service/");
alert(context );
</script>
</asp:Content>
But, the alert(context) can not execute ,when I check the concole it showed me TypeError: SP.ClientContext is not a constructor
,that is to say there was something wrong with the initialization of ClientContext. Why? How can I get ClientContext? Or was it caused by the lacking of SP.js?
但是,警报(上下文)无法执行,当我检查它显示给我的控制台时TypeError: SP.ClientContext is not a constructor
,也就是说 ClientContext 的初始化有问题。为什么?我怎样才能获得 ClientContext?还是因为缺少SP.js?
My Final Solution is : Add this statement to the masterpage: ,and then everything works good! May this can help you.
我的最终解决方案是:将此语句添加到母版页:,然后一切正常!愿这能帮到你。
回答by Mihir
Instead of writing directly in the <script>
blocks try to insert in the function
and call that function on body load.
不是直接在<script>
块中写入,而是尝试插入function
并在体负载上调用该函数。
Another good way is call using the jquery
另一个好方法是使用 jquery 调用
$(document).ready()(function(){
var context =new SP.ClientContext("http://sp2010dev1:88/Service/");
alert(context );
});
Try this once, even if it is not working then will try to find another solution.
尝试一次,即使它不起作用,也会尝试找到其他解决方案。
Along with this as per steve suggestion, include first sp.js
and perform remaining things.
根据史蒂夫的建议,sp.js
除此之外,首先包括并执行剩余的事情。
回答by Steve Mannina
Yes, it probably is because SP.js is not loaded. Try this:
是的,可能是因为没有加载 SP.js。试试这个:
<SharePoint:ScriptLink runat="server" Name="SP.js" Localizable="false" OnDemand="False" LoadAfterUI="True"></SharePoint:ScriptLink>
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(function () {
/* your code here */;
}, "sp.js");
</script>