$(document).ready(function() { $("button").click(function(){ $.aja...">

Why does the 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 should an AJAX request have a placeholder for this function? (in this case it is "data"). If you remove it or use any other word, it still works fine. Can someone explain why?

+3


source to share


1 answer


Yours data

here is an alias for the return value ("response" for your ajax request to script.php

), so you can reset it. It is NOT a placeholder for the function itself.

How you call it, it's up to you - just like with lambda parameter names in C ++ (I find them similar to JavaScript anonymous functions in this case):

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

      



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

For the C ++ analogy: what would it look like to pass a lambda as a parameter to another method (you would need to define a class Button

):

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

      

+1


source







All Articles