Javascript 未捕获的类型错误:$.ajax(...).success 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39042085/
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
Uncaught TypeError: $.ajax(...).success is not a function
提问by Karlom
I'm new to jQuery and using a little old tutorial on node.js
that uses this snippet :
我是 jQuery 的新手,并使用了一个node.js
使用此代码段的旧教程:
$(function () {
var roomId;
$.ajax({
type: "GET",
url: "/api/rooms"
}).success(function (rooms) {
roomId = rooms[0].id;
getMessages();
$.each(rooms, function (key, room) {
var a = '<a href="#" data-room-id="' + room.id + '" class="room list-group-item">' + room.name + '</a>';
$("#rooms").append(a);
});
});
[...]
});
However I get this error
但是我收到这个错误
Uncaught TypeError: $.ajax(...).success is not a function
未捕获的类型错误:$.ajax(...).success 不是函数
at }).success(function (rooms) {
在 }).success(function (rooms) {
I'm wondering what can be wrong here?
我想知道这里有什么问题?
回答by JCollerton
The call to ajax should look like:
对 ajax 的调用应如下所示:
$.ajax({
type: "GET",
url: "/api/rooms",
success: function (rooms) {
}
});
You don't method chain the success function, it is one of the entries in the dictionary argument.
您没有方法链接成功函数,它是字典参数中的条目之一。
回答by Shailesh Sonare
Your code is correct there is no problem with it
你的代码是正确的,没有问题
but you might be including the new jquery library which doesn't allow .success() method
但您可能包含不允许 .success() 方法的新 jquery 库
for newer version of jquery use
对于较新版本的 jquery 使用
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$.ajax({
type: "GET",
url: "/api/rooms",
success: function (rooms) {
}
});
</script>
and if you are using old jquery the .success() method would run without any problem
如果您使用的是旧的 jquery,则 .success() 方法将毫无问题地运行
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$.ajax({
url: "/api/rooms",
method: "GET",
data: {'datavar': datavalue}
}).success(function (rooms) {
console.log("successfully run ajax request..." + rooms);
}).done(function () {
console.log("I am from done function");
}).fail(function () {
console.log("I am from fail function.");
}).always(function () {
console.log("I am from always function");
});
</script>
回答by adeneo
According to the documentation
根据文档
The
jqXHR.success()
,jqXHR.error()
, andjqXHR.complete()
callback methods are removed as of jQuery 3.0.You can use
jqXHR.done()
,jqXHR.fail()
, andjqXHR.always()
instead.
的
jqXHR.success()
,jqXHR.error()
和jqXHR.complete()
回调方法也会被删除的jQuery 3.0。您可以使用
jqXHR.done()
,jqXHR.fail()
和jqXHR.always()
来代替。
These methods were originally added to jQuery's $.ajax
as options callbacks, to be used like this
这些方法最初是$.ajax
作为选项回调添加到 jQuery 的,像这样使用
$.ajax({
url : 'mypage.php',
success : function() { ... },
error : function() { ... },
complete : function() { ... }
});
However, due to some confusion among users, they were later also accompanied by chainable methods with the same names
但是,由于用户之间的一些混淆,他们后来也附上了具有相同名称的可链接方法
$.ajax().success( function() { ... })
.error( function() { ... })
.complete( function() { ... })
These methods have been deprecated since jQuery 1.8, and completely removed in jQuery 3.0, due to the use of Deferred objects, and later promises.
这些方法自 jQuery 1.8 起已被弃用,并在 jQuery 3.0 中完全删除,因为使用了 Deferred 对象,以及后来的承诺。
The jqXHR.success()
, jqXHR.error()
, and jqXHR.complete()
are superseeded by the chainable jqXHR.done()
, jqXHR.fail()
, and jqXHR.always()
methods, the options callbacks are still available for now.
的jqXHR.success()
,jqXHR.error()
以及jqXHR.complete()
由可链接superseeded jqXHR.done()
, jqXHR.fail()
和jqXHR.always()
方法,选项回调仍可用于现在。
As of jQuery 3.0, jQuery's Deferred objects are also Promise/A+ compliant, which means they are "thenable", and can be used with then()
as well
在jQuery 3.0,jQuery的递延对象也是无极/ A +兼容,这意味着它们是“thenable”,并且可以与被使用then()
,以及
$.ajax("/status").then(function(data) {
}).catch(function(error) {
});