jQuery TypeError: $.ajax(...) 不是函数?

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

TypeError: $.ajax(...) is not a function?

jqueryajaxjson

提问by ReactingToAngularVues

I'm trying to create a simple AJAX request which returns some data from a MySQL database. Here's my function below:

我正在尝试创建一个简单的 AJAX 请求,该请求从 MySQL 数据库返回一些数据。这是我的功能如下:

function AJAXrequest(url, postedData, callback) {
    $.ajax() ({
        type: 'POST',
        url: url,
        data: postedData,
        dataType: 'json',
        success: callback
    });
}

...and here's where I call it, parsing in the required parameters:

...这里是我调用它的地方,解析所需的参数:

AJAXrequest('voting.ajax.php', imageData, function(data) {
    console.log("success!");
});

Yet, my success callback does not run (as "success!" is not logged to the console), and I get an error in my console:

然而,我的成功回调没有运行(因为“成功!”没有记录到控制台),我的控制台出现错误:

TypeError: $.ajax(...) is not a function.
success: callback

What does this mean? I've done AJAX requests before where the success event triggers an anonymous function inside of $.ajax, but now I'm trying to run a separate named function (in this case, a callback). How do I go about this?

这是什么意思?在成功事件触发 $.ajax 中的匿名函数之前,我已经完成了 AJAX 请求,但现在我正在尝试运行一个单独的命名函数(在本例中为回调)。我该怎么做?

回答by Gus

Neither of the answers here helped me. The problem was: I was using the slim build of jQuery, which had some things removed, ajax being one of them.

这里的答案都没有帮助我。问题是:我使用的是 jQuery 的超薄版本,它删除了一些东西,ajax 就是其中之一。

The solution: Just download the regular (compressed or not) version of jQuery hereand include it in your project.

解决方案:只需在此处下载 jQuery 的常规(压缩或未压缩)版本并将其包含在您的项目中。

回答by karthiks

Double-check if you're using full-version of jquery and not some slim version.

仔细检查您是否使用完整版的 jquery 而不是一些精简版。

I was using the jquery cdn-script link that comes with jquery. The problem is this one by default is slim.jquery.js which doesn't have the ajaxfunction in it. So, if you're using (copy-pasted from Bootstrap website) slim version jquery script link, use the full version instead.

我正在使用 jquery 附带的 jquery cdn-script 链接。问题是默认情况下这个是 slim.jquery.js,其中没有该ajax函数。因此,如果您使用(从 Bootstrap 网站复制粘贴)精简版 jquery 脚本链接,请改用完整版。

That is to say use <script src="https://code.jquery.com/jquery-3.1.1.min.js">instead of <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"

也就是说用 <script src="https://code.jquery.com/jquery-3.1.1.min.js">代替 <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"

回答by Jason Evans

Not sure, but it looks like you have a syntax error in your code. Try:

不确定,但看起来您的代码中有语法错误。尝试:

$.ajax({
  type: 'POST',
  url: url,
  data: postedData,
  dataType: 'json',
  success: callback
});

You had extra brackets next to $.ajaxwhich were not needed. If you still get the error, then the jQuery script file is not loaded.

您旁边有不需要的额外括号$.ajax。如果仍然出现错误,则 jQuery 脚本文件未加载。

回答by Mahbub

Checkout The Jquery Documentation

签出Jquery 文档

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 abhinsit

There is a syntax error as you have placed parenthesis after ajax function and another set of parenthesis to define the argument list:-

存在语法错误,因为您在 ajax 函数和另一组括号之后放置了括号来定义参数列表:-

As you have written:-

正如你所写:-

$.ajax() ({
    type: 'POST',
    url: url,
    data: postedData,
    dataType: 'json',
    success: callback
});

The parenthesis around ajax has to be removed it should be:-

必须删除 ajax 周围的括号,它应该是:-

$.ajax({
    type: 'POST',
    url: url,
    data: postedData,
    dataType: 'json',
    success: callback
});

回答by netvision73

You have an error in your AJAX function, too much brackets, try instead $.ajax({

你的 AJAX 函数有错误,括号太多,试试吧 $.ajax({

回答by Daniel Mendoza

Reference the jquery min version that includes ajax:

参考包含 ajax 的 jquery min 版本:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

回答by Lily H.

If you are using bootstrap html template remember to remove the link to jquery slim at the bottom of the template. I post this detail here as I cannot comment answers yet..

如果您使用的是 bootstrap html 模板,请记住删除模板底部的 jquery slim 链接。我在这里发布这个细节,因为我还不能评论答案..

回答by Jianeng Xu

I encountered the same question, and my solution was: add the JQuery script.

我遇到了同样的问题,我的解决方案是:添加 JQuery 脚本。

Especially, we should make sure the corresponding JQuery is loaded when we debug our js under the firefox/chrome.

特别是我们在firefox/chrome下调试js的时候,要确保加载了对应的JQuery。

回答by bersling

For anyone trying to run this in nodejs: It won't work out of the box, since jquery needs a browser (or similar)! I was just trying to get the import to run and was logging console.log($)which wrote [Function]and then also console.log($.ajax)which returned undefined. I had no tscerrors and had autocomplete from intellij, so I was wondering what's going on.

对于任何试图在nodejs 中运行它的:它不会开箱即用,因为 jquery 需要一个浏览器(或类似的)!我只是想让导入运行,并记录console.log($)哪个写入[Function]然后console.log($.ajax)返回undefined. 我没有tsc错误并且从 intellij 自动完成,所以我想知道发生了什么。

Then at some point I realised that nodemight be the problem and not typescript. I tried the same code in the browser and it worked. To make it work you need to run:

然后在某些时候我意识到这node可能是问题而不是打字稿。我在浏览器中尝试了相同的代码,并且成功了。要使其工作,您需要运行:

require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }

    var $ = require("jquery")(window);
});

(credits: https://stackoverflow.com/a/4129032/3022127)

(学分:https: //stackoverflow.com/a/4129032/3022127