0

I'm using JSONP to retrieve values for a login form, then having the form self-submit. However, the form is submitting before the fields are populated, resulting in validation flags.

The function does return the correct value, but the globally scoped variable unscrambleText is returning undefined. I know that async:false is necessary to return synchronous calss, but that seems to be ignored in this case. I got this to work with a basic AJAX call on another page. What am I missing with this specific example?

$(document).ready(function () {
            if ((getQueryVariable("a") != 0) && (getQueryVariable("b") != 0)) {
                $("#UsernameTextBox").val(decryptEm(getQueryVariable("a")));
                $("#PasswordTextBox").val(decryptEm(getQueryVariable("b"));
                //This action is being triggered before the above two fields are populated
                $("#ctl00_ContentPlaceHolder1_SubmitButton").trigger("click");
            }

            function getQueryVariable(variable) {
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i = 0; i < vars.length; i++) {
                    var pair = vars[i].split("=");
                    if (pair[0] == variable) {
                        return unescape(pair[1]);
                    }
                }
                return 0;
            }

            function decryptEm(thevar) {
                var unscrambleText;

                $.ajax({
                    url: 'http://foo.bar/decrypt.php',
                    async: false,
                    dataType: 'jsonp',
                    data: 'text=' + thevar,
                    success: function (data) {
                        unscrambleText = data["theval"].replace(/^\s+|\s+$/g, "");
                    }
                });
                return unscrambleText;
            }

        });

3 Answers 3

0

you could try using setInterval before triggering the click, maybe a 2 second delay is enough.

alternately changing your code to do decryptEms, which would do all fields and return both, and only then you do your trigger form submit

2
  • You mean have decryptEm() parse both values at the same time? Would putting $(blah).trigger('click') inside decryptEm() solve this problem?
    – justacoder
    Oct 19, 2011 at 20:53
  • yes, both at the same time and then trigger click. Putting click inside the current function would not work as it would fire the first time, which you don't want.
    – Moin Zaman
    Oct 19, 2011 at 20:59
0

Its returning undefined because it has no value when the function returns. There is a delay between when decryptEm and the server responds to your request. This delay could be anything because it depends on the server response time.

0

Put this in a function, like this:

function loadValues(){
    $("#UsernameTextBox").val(decryptEm(getQueryVariable("a")));
    $("#PasswordTextBox").val(decryptEm(getQueryVariable("b"));
    return true;
}
if(loadValues==true){
    $("#ctl00_ContentPlaceHolder1_SubmitButton").trigger("click");
}

This way you can be sure the other stuff has finished before the submit is fired.

1
  • Nothing happens; decryptEm doesn't get called. I kept your code tweaks at the top of the code block. Does that matter?
    – justacoder
    Oct 19, 2011 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.