代码给出了错误,“ReferenceError:CryptoJS未定义”,而我已经包含了必需的.js引用,这是什么原因?

时间:2014-04-09 11:11:30

标签: javascript ajax json sharepoint-2013

这是我的代码,我已经包含了以下.js文件,onpage load它给出错误“ReferenceError:CryptoJS未定义”为什么在添加了js引用时它会给出错误。我使用Office 365制作了一个sharepoint-2013应用程序。

<script type="text/javascript" src="../Scripts/sha1.js"></script>
<script type="text/javascript" src="../Scripts/hmac-sha1.js"></script>

  'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();

(function () {

    // This code runs when the DOM is ready and creates a context object which is 
    // needed to use the SharePoint object model
    $(document).ready(function () 
    {
        getUserName();

        $("#button1").click(function()
        {
                paraupdate();   
        });

    });

    // This function prepares, loads, and then executes a SharePoint query to get 
    // the current users information

    function paraupdate()
    {

        var str=""+$("#textbox1").val();
        alert(""+str);
        var message = str+"json539ff0f815ca697c681fe01d32ba52e3";
        var secret = "<my private key>";        
        var crypto = CryptoJS.HmacSHA1(message, secret).toString();
        alert("crypto answer is " + crypto);
        var siteurl="http://pnrbuddy.com/api/station_by_code/code/"+str+"/format/json/pbapikey/539ff0f815ca697c681fe01d32ba52e3/pbapisign/"+crypto;


         $.ajax({
            url: siteurl,
            type: "GET",
            dataType: 'json',

            success: function (data) {
                     alert("IN Success");
                alert(""+data.station_by_code);

            },
            error: function (error) {
                alert("IN Error");
                alert(JSON.stringify(error));
            }
        });       


    }


    function getUserName() 
    {
        context.load(user);
        context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
    }

    // This function is executed if the above call is successful
    // It replaces the contents of the 'message' element with the user name
    function onGetUserNameSuccess() 
    {
        $("#label1").html("Enter Station Code : ");
        $("#button1").val("CLICK");

    }

    // This function is executed if the above call fails
    function onGetUserNameFail(sender, args) {
        alert('Failed to get user name. Error:' + args.get_message());
    }

})();

4 个答案:

答案 0 :(得分:11)

sha256.js

之前

包括 core-min.js

答案 1 :(得分:0)

有两种形式可以解决这个问题:

1:手动加载,我对这种模式有了更多的成功:

$.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js", execOperation);
            }
        );

示例:

$.getScript("~hostUrl/_layouts/15/SP.RequestExecutor.js", getListDataREST);

2:点播脚本:

SP.SOD.executeFunc('sp.userprofiles.js', 'SP.ClientContext', loadUserData);

此SharepointExchange发布,为大多数AppParts提供了通常的JSOM实现:Jquery is not firing on Page load SharePoint 2013

答案 2 :(得分:0)

错误已解决我添加了在线参考,

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script>

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>

答案 3 :(得分:0)

也许为时已晚,但是:

var CryptoJS = require('crypto-js');
var hash = CryptoJS.HmacSHA256("Message", "secret");
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
console.log(hashInBase64); // qnR8UCqJggD55PohusaBNviGoOJ67HC6Btry4qXLVZc=

在node.js中工作正常。