本文介绍了jQuery的错误:太多的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个文件上传系统实现使用CryptoJS客户端加密。

I'm trying to create a file upload system implementing client side encryption using CryptoJS.

我遇到的问题是,脚本的执行是由下面的错误在Firebug的控制台停止:太多的递归调用

The problem I'm having is that execution of the script is stopped by the following error in Firebug's console : too much recursion

我花了半天的时间试图解决这个问题,删除 VAR jqxhr = $就部分删除了错误,但会删除我的脚本发布功能。我曾尝试删除所有的加密线,分成不同的功能,但似乎没有做到这一点。任何jQuery的利弊知道是怎么回事了?

I have spent half of the day trying to resolve the problem, removing the var jqxhr = $.ajax part removes the error but removes posting functionality from my script. I have tried removing all the encryption lines, separating into different functions, but nothing seems to do it. Any jQuery pros know what's going wrong ?

这里的code:

$("#successmsg").hide();
$("#errormsg").hide();

function randomString(n)
{
    var text = '';
    var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    for(var i=0; i < n; i++)
    {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }

    return text;
}


$("#upload").submit(function(event) {
    event.preventDefault();

    input = document.getElementById('file');

    if(!input.files[0]) {
        $("#errormsg").html("No file selected.");
        $("#successmsg").hide();
        $("#errormsg").show();
    }

    fr = new FileReader();

    fr.onload = function() {
        var fname = input.files[0].name;
        var fkey = CryptoJS.SHA256(randomString(128));
        var skey = CryptoJS.SHA256(fkey);

        var fdata = CryptoJS.AES.encrypt(fr.result, "TestPassword");

        var jqxhr = $.ajax({
            url: "/api/files/upload",
            type: "POST",
            data: {
                'name': fname,
                'data': fdata,
                'key': skey
            },
            cache: false,
            dataType: 'json',
            processData: false
        });
    }

    fr.readAsText(input.files[0]);
});

下面是一个的jsfiddle: http://jsfiddle.net/wob66Lc0/

Here is a JSFiddle : http://jsfiddle.net/wob66Lc0/

推荐答案

问题是, CryptoJS 函数返回的对象不是字符串,所以你必须在尝试之前,字符串化它发送它。

The issue is that CryptoJS functions return objects not strings, so you have to stringify it before you attempt to send it.

    var jqxhr = $.ajax({
        url: "/api/files/upload",
        type: "POST",
        data: {
            'name': fname,
            'data': fdata.toString(),
            'key': skey.toString()
        }
    });

http://jsfiddle.net/wob66Lc0/1/

在字节另加密的作品不是文字,所以你应该阅读的文件作为二进制字符串代替文字

Also encryption works on bytes not text so you should read the file as a binary string instead of text

fr.readAsBinaryString(input.files[0]);

这篇关于jQuery的错误:太多的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:49