简体   繁体   English

未捕获的TypeError:$ .ajax(...)。error不是函数

[英]Uncaught TypeError: $.ajax(…).error is not a function

 <!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.php with incorrect extension to check for an error() and display it. 当我更改URL时, error()不起作用:to page.php ,扩展名不正确,以检查error()并显示它。

But, in console it displays an error saying: 但是,在控制台中它显示错误说:

Uncaught TypeError: $.ajax(...).error is not a function 未捕获的TypeError:$ .ajax(...)。错误不是函数

Since version 3.0, jQuery replaces error() with fail() for chained promise call. 从版本3.0开始,jQuery用fail()替换带有链接的promise调用的error() So you should use 所以你应该使用

$.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. 弃用注意:从jQuery 3.0开始,jqXHR.success(),jqXHR.error()和jqXHR.complete()回调将被删除。 You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead. 您可以使用jqXHR.done(),jqXHR.fail()和jqXHR.always()代替。

Maybe you are using the slim build of jquery which does not have ajax function. 也许你正在使用没有ajax功能的jquery的超薄版本。 Try to download the regular one from this link 尝试从此链接下载常规的

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. 如果这打印错误,那么你没有Ajax。 In this case, change you jquery to point to CDN like https://code.jquery.com/jquery-2.2.4.min.js . 在这种情况下,将jquery更改为指向CDN,如https://code.jquery.com/jquery-2.2.4.min.js

Secondly change you Ajax function to below. 其次将Ajax函数更改为以下。 Here i modified the error and complete function plus removed duplicate success function. 在这里,我修改了错误和完整功能,并删除了重复成功功能。

$.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*/
    }
});

You are using slim version of jQuery. 您正在使用瘦身版的jQu​​ery。 It Doesn't support ajax Calling. 它不支持ajax调用。 Use following cdn 使用以下cdn

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM