如何在文档加载时立即进行AJAX调用(How to make an AJAX call immediately on document loading)

编程入门 行业动态 更新时间:2024-10-28 00:24:02
如何在文档加载时立即进行AJAX调用(How to make an AJAX call immediately on document loading)

我想在加载文档后立即执行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>

更多推荐

本文发布于:2023-04-29 04:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   文档   如何在   AJAX   loading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!