发送没有回调的帖子,得到太多的递归

时间:2012-11-05 19:44:39

标签: javascript jquery

所以我正在处理一个预先存在的网站,而我正试图添加代码发送一些数据的点。当它到达我的.post()时,我收到以下错误:

too much recursion
http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
Line 16

这是流程:

某些链接文本具有以下onclick处理程序:

<a class="lucida_12pxBlueLH16" onclick="changeCipherAdmin(6); return false;" href="#">Keyword: SLEEP</a>

function changeCipherAdmin(num) {
    tempHTML = '';
    tempHTML += '<select id="select_cipher' + num + '" name="select_cipher' + num + '" class="select_tanbg" onchange="updateKeyFieldsAdmin(this.value,' + num + ',1);">';
    tempHTML += '       <option id="option' + num + '_admin1" value="1">Additive</option>';
    tempHTML += '       <option id="option' + num + '_admin2" value="2">Affine</option>';
    tempHTML += '       <option id="option' + num + '_admin3" value="3">Caesar</option>';
    tempHTML += '       <option id="option' + num + '_admin4" value="4">Keyword</option>';
    tempHTML += '       <option id="option' + num + '_admin5" value="5">Multiplicative</option>';
    tempHTML += '       <option id="option' + num + '_admin6" value="6">Vigen&egrave;re</option>';
    tempHTML += '</select>';
    document.getElementById('admin_cipher' + num).innerHTML = tempHTML;
    document.getElementById('option' + num + '_admin' + ciphers[num]).selected = true;
    updateKeyFieldsAdmin(ciphers[num], num, 0);
}

基于它,它运行以下功能

function updateKeyFieldsAdmin(cipherNum, selectNum, resetNum) {
                //tempHTML='<a href="#" onclick="changeCipherAdmin('+selectNum+'); return false;" class="lucida_12pxBlueLH16">'+possible_ciphers[cipherNum]+'</a>';
                //document.getElementById('admin_cipher'+selectNum).innerHTML=tempHTML;

                if (resetNum == 0) {
                    keyA = keysA[selectNum];
                    keyB = keysB[selectNum];
                }
                if (cipherNum == 1) {
                    //0-25
                    //change letter to number, add encryption key (two characters?), reduce mod 26
                    //additive: use a:number
                    if (resetNum == 1) {
                        keyA = "";
                        keyB = "";
                    }

                    tempHTML = '<strong class="helvetica11pxTanB">Key&#0160;(0-25)</strong> <input type="text" id="key_a' + selectNum + '" maxlength="2" class="form_field11px" style="width:19px; height:12px; text-align:right; color:#000000;" value="' + keyA + '" onkeyup="checkKeysAdmin(1,' + selectNum + '); return false;" autocapitalize="off" autocorrect="off" />';
                }
                else if (cipherNum == 6) {
                    //vigenere: use a:word--26 letters or less
                    if (resetNum == 1) {
                        keyA = "";
                        keyB = "";
                    }

                    tempHTML = '<strong class="helvetica11pxTanB">Keyword</strong> <input type="text" id="key_a' + selectNum + '" maxlength="26" class="form_field11px" style="width:99px; height:12px; text-align:right; color:#000000;" value="' + keyA + '" onkeyup="checkKeysAdmin(event,6,' + selectNum + '); return false;" autocapitalize="off" autocorrect="off" />';
                }
                document.getElementById('admin_key' + selectNum).innerHTML = tempHTML;

                if ((cipherNum == 2 || cipherNum == 5) && !isNaN(keyA) && keyA != "") {
                    //update select field
                    if (cipherNum == 2) {
                        $('#key_a' + selectNum).val(keyA);
                    }
                    else {
                        for (i = 1; i < odd_nums_prime26.length; i++) {
                            if (keyA * 1 == odd_nums_prime26[i]) {
                                document.getElementById('key_a' + selectNum).selectedIndex = i;
                                document.getElementById('option' + selectNum + '_mult' + i).selected = true;
                                break;
                            }
                        }
                    }
                }
                if (resetNum == 1) {
                    checkKeysAdmin(cipherNum, selectNum);
                }
            }

然后调用以下内容:

function checkKeysAdmin(e, cipherNum, row) {
                encrypt_ready = true;

                if (encrypt_ready == true) {
                    //keyA and keyB should already be updated...so:
                    keysA[row] = keysA;
                    keysB[row] = keysB;
                    ciphers[row] = cipherNum;
                    ciphertext[row] = encryptTextAdmin(plaintext[row], cipherNum);
                    document.getElementById('cipher' + row).innerHTML = ciphertext[row];

                    // This is my code where Im trying to send my data out
                    if (e.keyCode == 13 ) {
                        alert( 'here2' );
                        $.post('/challenges/save.php', { action:'updateJokeMessage',
                            messageId:message_ids[row],
                            joke:message_text[row],
                            punchline:plaintext[row],
                            encryptedPunchline:ciphertext[row],
                            cipherId:cipherNum,
                            keyA:keysA[row],
                            keyB:keysB[row]
                        });
                        alert( 'Done' );
                    }


                    return;
                }
                else {
                    //alert("not ready to encrypt");
                    document.getElementById('cipher' + row).innerHTML = '';
                }
                // me trying to stop the recursion
                event.stopPropagation();
            }

我已经尝试添加回调并放入event.stopPropagation或返回等。但无法弄清楚原因。任何想法/帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

所以它最终成了变形错误:

keysA[row] = keysA;
keysB[row] = keysB;

这基本上是将对象分配给它的一个元素,当jquery尝试处理时会导致递归。

答案 1 :(得分:0)

我强烈怀疑你的问题在这里:

keysA[row] = keysA;
keysB[row] = keysB;

你正在制作一个循环结构,当jQuery试图通过参数对象追踪时,jQuery正在失去理智。