1

Hey guys so am testing this cracker am working on via localhost to see if i can do it really since am bored and want to do something so i thought about this but may i ask why break and continue arnt working its a for loop isnt it?, So it should work.

Edit: Forgot to mention that the code doesnt even work when i added the break and continue.

Any help is great thanks.

function doTest() {

    var html_next;

    var user_l = document.getElementById("users");
    var pass_l = document.getElementById("pass");

    if (user_l.value == "") {
        alert("The username field cant be empty!")
    } else if (pass_l.value == "") {
        alert("The password field cant be empty!")
    }

    var message = document.getElementById('status_1');

    var user_l_s = user_l.value.split("\n");
    var pass_l_s = pass_l.value.split("\n");


    for (var i = 0; i < user_l_s.length; i++) {
        num_users++;
        for (var j = 0; j < pass_l_s.length; j++) {
            $.ajax({
                url: 'posttest.php',
                type: 'GET',
                data: {'users': user_l_s[i], 'pass': pass_l_s[j]},
                dataType: 'text',
                async: false,
                success: (function (i, j) {

                    return function (response) {
                        html_next = response;

                        if (html_next.indexOf("Failed") > -1) {
                            continue;

                        } else if (html_next.indexOf("Cracked") > -1) {
                            break;

                        } else if (html_next.indexOf("DELETED") > -1) {
                            break;
                        }
                    }
                })(i, j),
                beforeSend: function () {
                    message.innerHTML = "Cracking Test...";
                }
            });
        }
    }
    message.innerHTML = "Done...";
}
BSX IOS
  • 25
  • 6
  • 2
    `break` and `continue` are placed _inside function, which is inside success function_, so there is nothing to break. – Regent Apr 17 '15 at 07:51
  • But then how would i do it if it's not inside there any example or something? – BSX IOS Apr 17 '15 at 07:54
  • You could perform each request synchronously one after the other, the response handler of one calling the next. based on the response, you could just not call the next request in line. – Matt Apr 17 '15 at 07:56
  • I think so you might be getting error Uncaught SyntaxError: Illegal break statement – Kushal Apr 17 '15 at 07:57
  • @BSXIOS if you are bored, why not to read some tutorials or books to learn about _how all these work_? – Regent Apr 17 '15 at 08:00
  • Well there are tutorials i don't really understand so i have to try it myself to understand it. – BSX IOS Apr 17 '15 at 08:03
  • possible duplicate of [Multiple ajax calls when previous one completes](http://stackoverflow.com/questions/25936196/multiple-ajax-calls-when-previous-one-completes) – Matt Apr 17 '15 at 08:12

3 Answers3

0

It is because you are sending an Async request to your server. Which means your server is handling multiple requests and it is not necessary that every request gets responded to in the same time.

What you are looking for is sequential code that can process your data all at once.

You should try async:false and then try

progrAmmar
  • 2,606
  • 4
  • 29
  • 58
  • As of jQuery 1.8, the use of `async: false` is deprecated. It does not work in some browsers (like ipad's safari) – kosmos Apr 17 '15 at 08:02
0

You have to use success callbacks, you just need to use other logic. Instead for loops, call to a function when the ajax success is complete. This function have to register the changes and, if necessary, stop or keep working.

Here is an example that I used with another SO user but the logic and the idea are the appropriate. Instead of using loops, we wait for each success callback to keep working.

var MyApp = {

    Scripts: {

        ToLoad: [
            '/path/to/script1.js',
            '/path/to/script2.js',
            '/path/to/script3.js'
        ],

        Loaded: 0

    },

    DoTheJob: function(){
        if( this.Scripts.ToLoad.length == this.Scripts.Loaded ) {
            // do some stuff
            return;
        }
        $.getScript(this.Scripts.ToLoad[this.Scripts.Loaded], function(){
            MyApp.Scripts.Loaded++;
            MyApp.DoTheJob();
        });
    }

};

$(function(){ 
    MyApp.DoTheJob();
});

Here is the jsFiddle

kosmos
  • 4,253
  • 1
  • 18
  • 36
0

This should do it.. You can even try it out with async = true I think so it should work with it too

function doTest() {

    var html_next;

    var user_l = document.getElementById("users");
    var pass_l = document.getElementById("pass");

    if (user_l.value == "") {
        alert("The username field cant be empty!")
    } else if (pass_l.value == "") {
        alert("The password field cant be empty!")
    }

    var message = document.getElementById('status_1');

    var user_l_s = user_l.value.split("\n");
    var pass_l_s = pass_l.value.split("\n");

    for (var i = 0; i < user_l_s.length; i++) {
        num_users++;

        function makeReq(pass_l_s, index) {
            var index = index || 0;
            $.ajax({
                url: 'posttest.php',
                type: 'GET',
                data: {
                    'users': user_l_s[i],
                    'pass': pass_l_s[index]
                },
                dataType: 'text',
                async: false,
                success: (function(i, index) {

                    return function(response) {
                        html_next = response;

                        if (html_next.indexOf("Failed") > -1) {
                            index += 1
                            if (index < pass_l_s.length)
                                makeReq(pass_l_s, index)
                                // continue;

                        } else if (html_next.indexOf("Cracked") > -1) {
                            // break;

                        } else if (html_next.indexOf("DELETED") > -1) {
                            // break;
                        }
                    }
                })(i, index),
                beforeSend: function() {
                    message.innerHTML = "Cracking Test...";
                }
            });
        }
        makeReq(pass_l_s)
    }
    message.innerHTML = "Done...";
}
Kushal
  • 1,360
  • 6
  • 16