How to make an AJAX call immediately on document loading

Posted by Ankur on Stack Overflow See other posts from Stack Overflow or by Ankur
Published on 2010-05-10T13:31:14Z Indexed on 2010/05/10 13:34 UTC
Read the original article Hit count: 256

Filed under:
|
|

I want to execute an ajax call as soon as a document is loaded. What I am doing is loading a string that contains data that I will use for an autocomplete feature. This is what I have done, but it is not calling the servlet.

I have removed the calls to the various JS scripts to make it clearer. I have done several similar AJAX calls in my code but usually triggered by a click event, I am not sure what the syntax for doing it as soon as the document loads, but I thought this would be it (but it's not):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
  $(document).ready(function(){

          $.ajax({
                type: "GET",
                url: "AutoComplete",
                dataType: 'json',
                data: queryString,
                success: function(data) {
                        var dataArray = data;
                        alert(dataArray);
                        }
                });

     $("#example").autocomplete(dataArray);
  });
</script>
<title></title>
</head>
<body>
    API Reference:
<form><input id="example"> (try "C" or "E")</form>
</body>
</html>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX