我想在加载文档后立即执行ajax调用。 我在做的是加载一个包含我将用于自动完成功能的数据的字符串。 这就是我所做的,但不是调用servlet。
我已经删除了对各种JS脚本的调用,使其更清晰。 我在我的代码中完成了几个类似的AJAX调用,但通常是由点击事件触发的,我不确定文档加载时执行的语法是什么,但我认为这将是它(但事实并非如此):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="../js/jquery.js" type="text/javascript"> </script> <link rel="stylesheet" href="../css/styles.css" type="text/css"> <link rel="stylesheet" href="../css/jquery.autocomplete.css" type="text/css"> <script type="text/javascript" src="../js/jquery.bgiframe.min.js"> </script> <script type="text/javascript" src="../js/jquery.dimensions.js"> </script> <script type="text/javascript" src="../js/jquery.autocomplete.js"> </script> <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>编辑:我的代码现在看起来更像卡里姆的:
$(document).ready(function(){ $.ajax({ type: "GET", url: "../AutoComplete", success: function(data) { $("#example").autocomplete(data); } }); });尽管如此,自动完成仍然不起作用(不可否认,完全是另一个问题,所以我也会发布另一个问题,因此它有一个适当的标题)。
我发送的变量“数据”看起来像......“手稿|文本|信息对象|篮球|球|运动设备|狼蛛”.split(“|”);
如果我做
var dataArray = "Manuscript|Text|Information Object|Basketball|Ball|Sporting Equipment|Tarantula".split("|");接着
$("#example").autocomplete(dataArray);它一切正常,但是当我从服务器获取dataArray的值时,它不会。
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 src="../js/jquery.js" type="text/javascript"> </script> <link rel="stylesheet" href="../css/styles.css" type="text/css"> <link rel="stylesheet" href="../css/jquery.autocomplete.css" type="text/css"> <script type="text/javascript" src="../js/jquery.bgiframe.min.js"> </script> <script type="text/javascript" src="../js/jquery.dimensions.js"> </script> <script type="text/javascript" src="../js/jquery.autocomplete.js"> </script> <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>EDIT: my code now looks more like Karim's:
$(document).ready(function(){ $.ajax({ type: "GET", url: "../AutoComplete", success: function(data) { $("#example").autocomplete(data); } }); });Nonetheless the autocomplete still doesn't work (admittedly another question altogether, so I will also post another question so it has an appropriate title).
My variable "data" that is being sent back looks like ... "Manuscript|Text|Information Object|Basketball|Ball|Sporting Equipment|Tarantula".split("|");
If I do
var dataArray = "Manuscript|Text|Information Object|Basketball|Ball|Sporting Equipment|Tarantula".split("|");and then
$("#example").autocomplete(dataArray);It all works fine, but when I get the value of dataArray from the server it doesn't.
最满意答案
在调用jQuery API之前,您需要先加载jQuery。
在你的代码片段之前,加载jQuery ...
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>You need to have jQuery loaded before making calls to the jQuery API.
Before your code snippet, load jQuery...
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>更多推荐
发布评论