javascript 未捕获的类型错误:$.ajax(...).error 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47974868/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:43:26  来源:igfitidea点击:

Uncaught TypeError: $.ajax(...).error is not a function

javascriptjqueryhtmlajax

提问by Ameen Shaikh

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>jquery examples - 4</title>
 <link rel="stylesheet" type="text/css" href="css/style2.css">
</head>
<body>

<input id="name" type="text" name="">
<input id="button" type="button" value="load" name="">
<div id="content"></div>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/selectors12.js"></script>

</body>
</html>

$('#button').click(function() { 
var name = $('#name').val();

    $.ajax({
     url:'php/page.php',
     data: 'name='+name, //sending the data to page.php
     success: function(data) {  
      $('#content').html(data);

     }
     
    }).error(function() { 
     alert('an error occured');

    }).success(function() { 
     /*alert*/
     alert('an error occured');
    }).complete(function() { 
     /*alert*/
    });
});

the error()is not working, when I change the URL: to page.phpwith incorrect extension to check for an error()and display it.

error()不工作,当我改变的网址:以page.php不正确的扩展以检查error()和显示。

But, in console it displays an error saying:

但是,在控制台中,它显示一条错误消息:

Uncaught TypeError: $.ajax(...).error is not a function

未捕获的类型错误:$.ajax(...).error 不是函数

回答by Zamrony P. Juhara

Since version 3.0, jQuery replaces error()with fail()for chained promise call. So you should use

自从3.0版本,jQuery的替换error()fail()的链式承诺通话。所以你应该使用

$.ajax({ 
    .... 
}).done(function(resp){
    //do something when ok
}).fail(function(err) {
    //do something when something is wrong
}).always(function() {
   //do  something whether request is ok or fail
});

From jQuery Ajax documentation:

来自jQuery Ajax 文档

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

弃用通知:jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调从 jQuery 3.0 开始被移除。您可以改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。

回答by launj

Maybe you are using the slim build of jquery which does not have ajax function. Try to download the regular one from this link

也许您正在使用没有 ajax 功能的 jquery 的超薄版本。尝试从此链接下载常规的

回答by launj

You need to do two things here.

你需要在这里做两件事。

First make sure Ajax is there in your Jquery file using below code.

首先使用以下代码确保 Jquery 文件中存在 Ajax。

<script>
    $(document).ready(function() {
      console.log($.ajax);
    });
  </script>

If this prints error, then you don't have Ajax. In this case, change you jquery to point to CDN like https://code.jquery.com/jquery-2.2.4.min.js.

如果这打印错误,那么您没有 Ajax。在这种情况下,将您的 jquery 更改为指向 CDN,例如https://code.jquery.com/jquery-2.2.4.min.js

Secondly change you Ajax function to below. Here i modified the error and complete function plus removed duplicate success function.

其次将您的 Ajax 函数更改为下面的内容。在这里,我修改了错误和完整功能,并删除了重复的成功功能。

$.ajax({
    url:'php/page.php',
    data: 'name='+name, //sending the data to page.php
    success: function(data) {  
        $('#content').html(data);
    }, error : function(e) { 
        alert('an error occured');
    }, complete : function() { 
        /*alert*/
    }
});

回答by santosh singh

You are using slim version of jQuery. It Doesn't support ajax Calling. Use following cdn

您正在使用 jQuery 的精简版。它不支持ajax调用。使用以下cdn

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>jquery examples - 4</title>
    <link rel="stylesheet" type="text/css" href="css/style2.css">


</head>
<body>


<input id="name" type="text" name=""> <input id="button" type="button" value="load" name="">


        <div id="content"></div>

    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="js/selectors12.js"></script>

</body>
</html>