最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Code is giving error, "ReferenceError: CryptoJS is not defined" , while I have included required

programmeradmin2浏览0评论

Here is my code, I have included following .js files, onpage load it is giving error "ReferenceError: CryptoJS is not defined" why does it give that error when already js references are added. I am making a sharepoint-2013 app using office 365.

<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="/"+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());
    }

})();

Here is my code, I have included following .js files, onpage load it is giving error "ReferenceError: CryptoJS is not defined" why does it give that error when already js references are added. I am making a sharepoint-2013 app using office 365.

<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());
    }

})();
Share Improve this question edited Apr 9, 2014 at 11:30 Niranjan Kulkarni asked Apr 9, 2014 at 11:11 Niranjan KulkarniNiranjan Kulkarni 1711 gold badge1 silver badge10 bronze badges 1
  • From your code it is not possible to tell why CryptoJS is not defined. Show a complete example. – phylax Commented Apr 9, 2014 at 11:24
Add a comment  | 

4 Answers 4

Reset to default 11

include core-min.js before sha256.js

There are one of two forms for fixing this:

1: Manual Load, i have more success with this pattern:

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

Example:

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

2: Script on Demand:

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

This SharepointExchange posting, gives the usual JSOM implementation for most AppParts: Jquery is not firing on Page load SharePoint 2013

Error solved I added online references instead,

<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>

Maybe is too late, but:

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

Works fine in node.js.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论