jQuery & Ajax:ReferenceError: ajax 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18963022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery & Ajax: ReferenceError: ajax is not defined
提问by user2807746
I get a
我得到一个
ReferenceError: ajax is not defined
参考错误:ajax 未定义
error in browser console when I try to make an ajax call.
当我尝试进行 ajax 调用时,浏览器控制台中出现错误。
I'm pretty sure I properly loaded the jQuery library, therefore I don't understand how can the $.ajax function not be defined.
我很确定我正确加载了 jQuery 库,因此我不明白如何不定义 $.ajax 函数。
Here is the HTML (without irrelevent css and markup):
这是 HTML(没有无关的 css 和标记):
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<div>
<a class="getUsersA">Get users</a>
<div id="gridD"></div>
</div>
</body>
</html>
Here is the script.js file:
这是 script.js 文件:
$(document).ready(function() {
$(".getUsersA").click(function() {
$.ajax({
url:ajax/getUsers.php,
type:POST,
data:({
id:0
}),
success:function(results) {
$("#gridD").html(results);
}
});
});
});
Thank you for any help!
感谢您的任何帮助!
回答by karthikr
You need to wrap ajax/getUsers.php,
in quotes. or else it would look for a local variable ajax
rather than treating it as a string.
你需要ajax/getUsers.php,
用引号括起来。否则它会寻找一个局部变量ajax
而不是将其视为字符串。
url: "ajax/getUsers.php",
Something like this:
像这样的东西:
$.ajax({
url: 'ajax/getUsers.php',
type: 'POST',
data:({
id: 0
}),
success:function(results) {
$("#gridD").html(results);
}
});
回答by Curt
You have a syntax error on this line:
您在这一行有语法错误:
url:ajax/getUsers.php,
Change this to:
将此更改为:
url:"ajax/getUsers.php",
回答by Anthony Grist
You're supposed to be passing a string (or a variable containing a string) as the url
option, but you're instead doing:
您应该传递一个字符串(或包含字符串的变量)作为url
选项,但您正在执行以下操作:
url:ajax/getUsers.php,
Change it to:
将其更改为:
url:'ajax/getUsers.php',
What you have right now is looking for the variable ajax
(which doesn't exist) then trying to divide it by the php
property of the object referenced by getUsers
(which also doesn't exist), and then set the result as the value for the url
option.
您现在所拥有的是寻找变量ajax
(不存在),然后尝试将其除以php
引用的对象的属性getUsers
(也不存在),然后将结果设置为url
选项的值.