【问题标题】:Error: SP.ClientContext().get_current throws uncaught typeError: undefined is not a function错误:SP.ClientContext().get_current throws uncaught typeError: undefined is not a function
【发布时间】:2014-05-09 22:43:10
【问题描述】:

我正在关注这个会议研讨会http://video.ch9.ms/sessions/spc/2014/SPC261.mp4 他们教你如何创建共享点应用程序

有时我有这个脚本

$(document).ready(function () {

    //get context and then username
    context = new SP.ClientContext.get_Current();
    web = context.get_web();

    //get the tasks for the user
    getTaskCountForUser();


});

它只会计算任务列表中剩余的任务数。

我在应用程序部分的 aspx 文件中包含下一个脚本

<%--<script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>--%>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script src="/_layouts/15/sp.js" type="text/javascript"></script>


<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.runtime.debug.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.debug.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.0/jquery-ui.min.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js" type="text/javascript"></script>
<script src="../Scripts/QuickTask.js" type="text/javascript"></script>

一切似乎都很好。应用程序部分显示在 SharePoint Online 中,我可以将其包含在主页中,但脚本无法正常工作并在行中抛出 Uncaught TypeError: undefined is not a function

context = new SP.ClientContext.get_Current();

我尝试像这样封闭该段:

ExecuteOrDelayUntilScriptLoaded(function () {
   //my code above in here

}, "sp.js");

但在这种情况下,第一行会抛出相同的错误。

我错过了什么?我正在使用我的 MSDN 订阅 Vsual Studio 2013 中的 Sharepoint E3 在线帐户,在视频中,这个人可以无缝地完成它。

谢谢

【问题讨论】:

    标签: javascript sharepoint visual-studio-2013 sharepoint-2013


    【解决方案1】:

    您的代码中有一些拼写错误、调试 js 文件的链接(不是说这不正确,但为什么需要它们?)以及获取 ClientContext 对象时出错。

    要修复脚本包含问题,请使用以下脚本标签而不是您的:

    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js" ></script>
    <script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.js" ></script>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.0/jquery-ui.min.js" ></script>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js" ></script>
    

    但主要问题出在这一行:

    context = new SP.ClientContext.get_Current();
    

    在这种情况下,您不需要使用 new 关键字,您正在使用 getter 获取上下文对象。 应该是:

    context = SP.ClientContext.get_current();
    

    还要注意 get_current() 函数的大小写 - current 单词应该从小写字符开始。

    【讨论】:

    • 我只是改变了 get_current 方法的大小写,它就起作用了。在这样一个 n00b 的错误中花费这么长的时间真是太难过了。您的其余建议我将在部署和清理代码时实施。
    猜你喜欢
    • 1970-01-01
    • 2014-08-15
    • 2015-01-14
    • 2014-07-06
    • 2014-09-01
    • 2015-11-04
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多