javascript 未捕获的类型错误:当我尝试将数据发送到 url 时,无法读取未定义的属性“ajax”

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

Uncaught TypeError: Cannot read property 'ajax' of undefined When I'm trying to send data to url

javascriptjqueryajaxhtml

提问by

I have created a form to get feedback from user, I'm simply trying to send form data to url, but I'm getting this error:

我创建了一个表单来获取用户的反馈,我只是想将表单数据发送到 url,但我收到此错误:

Uncaught TypeError: Cannot read property 'ajax' of undefined

未捕获的类型错误:无法读取未定义的属性“ajax”

function sendData(){
    $.ajax({
        url: "www.yashkjhsah.php",
        type: "POST",
        async: true,
        data: $(".contacts_form").serialize(),
        dataType: "html",
        success: function(data) 
        {
            alert(data);
            if(data!="Error in Insert Query!")
            {
                alert("Thank you for Enquiry we will send answer in your Mail.");
            }
            else
            {
                alert("Error while saving the data");
            }
        }
    });
}

回答by TGrif

The error message says that jQuery is not define. You must include jQuery before doing anything with $.ajax

错误消息说 jQuery 未定义。在使用 $.ajax 执行任何操作之前,您必须包含 jQuery

Put this line in the html page before your script : <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

将此行放在脚本之前的 html 页面中: <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

回答by gassio

I had the same problem and I solved it, by changing the dollar symbol $ with jQuery.

我遇到了同样的问题,我通过使用jQuery更改美元符号 $ 来解决它。

 jQuery.ajax({
                type: "POST",
                url: "...",
                data: jQuery('myForm').serialize(),
                success: function(msg) {
                    console.log('success!');
                },
            });

回答by Manjar

That means that jquery has not been loaded.

这意味着尚未加载 jquery。

Make sure that you have the script in your html, and also wrap the call to this function sendDatainside a

确保您的 html 中有脚本,并将对此函数的调用包装sendData在一个

$(document).ready(function(){
//do stuff
})