如何使一个AJAX调用立即在文件加载 [英] How to make an AJAX call immediately on document loading

查看:102
本文介绍了如何使一个AJAX调用立即在文件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只要一个文件被加载执行Ajax调用。我在做什么是加载包含,我将使用自动完成功能数据的字符串。这是我做了什么,但他并没有叫这个servlet。

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.

我已删除了调用各种JS脚本,以使其更清晰。我有一个click事件在我的code做了几个类似的AJAX调用,但通常引发的,我不知道是什么语法,尽快做的文件加载,但我认为这将是它(但它不是)

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>

编辑:我的code现在看起来更像是卡里姆的:

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).

我的变量数据被送回的样子......手稿|文|信息对象|篮球|球|运动器材|狼蛛.split(|);

My variable "data" that is being sent back looks like ... "Manuscript|Text|Information Object|Basketball|Ball|Sporting Equipment|Tarantula".split("|");

如果我这样做

var dataArray = "Manuscript|Text|Information Object|Basketball|Ball|Sporting Equipment|Tarantula".split("|");

然后

$("#example").autocomplete(dataArray);

这一切工作正常,但是当我从服务器获取dataArray中的值,它没有。

It all works fine, but when I get the value of dataArray from the server it doesn't.

推荐答案

您需要有jQuery的打电话给jQuery的API之前加载。

You need to have jQuery loaded before making calls to the jQuery API.

您code段之前,加载的jQuery ...

Before your code snippet, load jQuery...

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>

这篇关于如何使一个AJAX调用立即在文件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆