Daniel
Daniel

Reputation: 3272

Why Does An AJAX Request Have To Have This?

<script type="text/javascript">
        $(document).ready(function() {
            $("button").click(function(){
                $.ajax({
                    type: 'POST',
                    url: 'script.php',
                    success: function(data) {
                    alert(data);
                     $("p").text(data);
                    }
                });
            });
        });
</script>

why does an AJAX request have to have a placeholder for the function? (in this case it is 'data'). If you remove it, or use any other word it still works fine. Can someone please explain why?

Upvotes: 3

Views: 68

Answers (1)

PolGraphic
PolGraphic

Reputation: 3364

Your data here is an alias for returned value (the "answer" for your ajax request to script.php), so you can reefer to it. It is NOT the placeholder for function itself.

How you name it is up to you - just like with names of lambda parameters in c++ (I find them similar to JavaScript anonymous functions in that case):

[](string data){
   ... = data...
}

or with "out" parameters of functions/methods in other languages.

For the C++ analogy: how it would look to pass an lambda as a parameter to another method (you would have to define the Button class of course):

button.click(/*...,*/ [&](string data){ //"on success"
    MessageBox(NULL, data.c_str(), "Alert", NULL);
    ...
});

Upvotes: 1

Related Questions