简体   繁体   English

javascript array.push返回未定义

[英]javascript array.push returns undefined

So I keep adding onto an array and push works but all that happens is I get ITERATIONS many undefined objects in the array. 因此,我不断添加到数组中并进行推送,但是所有发生的事情是我在数组中得到了很多未定义的对象。 The object I keep trying to push works fine when i use it on its own but when i try to put it in an array i get undefined 我一直尝试推送的对象在单独使用时可以正常工作,但是当我尝试将其放入数组时却无法定义

function multiCalc() {
    primeList = [];
    for (var i = 0; i < (iterations * 2); i += 2) {
        isPrime = true;
        var itTest = bigInt(test).add(i);

        for (var j = bigInt(itTest.divide(2).add(1)); j.compare(2) == 1; j = j.minus(1)) {
            if ((test.mod(j)) == 0) {
                isPrime = false;
            }
        }
        primeList.push({
            "prime_number": {
                "testNumber": itTest.toString(),
                "isPrime": isPrime,
                "wasTested": true
            }
        });

    }
    sendPrime(primeList, multiUrl);
}

thanks in advance 提前致谢

edit here is the send prime function 在这里编辑是发送主要功能

 function sendPrime(PrimeData, path){ 
    if(stop == false){
    $.ajax({
        url: path,
        type: 'post',
        async: true,
        dataType: 'json',
        success: function (data) {
        test = bigInt(data.testNumber);
        multiCalc();
        // calc();
        },
        data: PrimeData
    });
}

Ok I figured it out. 好吧,我知道了。 The array wasn't getting sent via ajax. 该数组未通过Ajax发送。 I stringified the data and then added the contentType: 'application/json', line so that rails would know it was receiving json 我对数据进行了字符串化,然后添加了contentType: 'application/json',行,以便Rails知道它正在接收json

function sendPrime(primeData, path){ 
    if(stop == false){
        console.log(primeData);
    $.ajax({
        url: path,
        type: 'post',
        async: true,
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
        test = bigInt(data.testNumber);
        console.log(test);
        multiCalc();
        // calc();
        },
        data: JSON.stringify(primeData)
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM