首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax请求在jQuery中不是函数错误

Ajax请求在jQuery中不是函数错误
EN

Stack Overflow用户
提问于 2019-10-04 15:41:09
回答 1查看 133关注 0票数 0

我试图使用验证器返回的ajax请求来显示我的验证错误,但是控制台一直返回这个错误。是不是因为我的jQuery版本(3.4.1)?

JS控制台输出

代码语言:javascript
复制
TypeError: $.ajax(...).done(...).error is not a function

Ajax代码

代码语言:javascript
复制
$(document).ready(function() {
    // Validation for all field
    $(".guest-query-submit").click(function (e) {
        e.preventDefault(); //Prevent default action

        //Define inputs
        let _token = $("input[name='_token']").val();
        let title = $("input[name='title']").val();
        let description = $("input[name='description']").val();
        let category = $("input[name='category']").val();
        let attachment = $("input[name='attachment']").val();
        let county = $("input[name='county']").val();
        let startDate = $("input[name='startDate']").val();
        let endDate = $("input[name='endDate']").val();
        let flexibility = $("input[name='flexibility']").val();
        let contactName = $("input[name='contactName']").val();
        let contactEmail = $("input[name='contactEmail']").val();
        let contactNumber = $("input[name='contactNumber']").val();

        //Starting ajax request
        $.ajax({
            url: "{{route('send-guest-query')}}",
            type: 'POST',
            data: {
                _token:_token,
                title:title,
                description:description,
                category:category,
                attachment:attachment,
                county:county,
                startDate:startDate,
                endDate:endDate,
                flexibility:flexibility,
                contactName:contactName,
                contactEmail:contactEmail,
                contactNumber:contactNumber },
            dataType: "json"
        }).done (function (data) {
            if(data.status === 200) {
                Swal.fire({ //SweetAlert
                    title: 'Success',
                    text: 'Your query was sent successfully!',
                    type: 'success',
                    confirmButtonText: 'Continue'
                })
            }}).error(function (data) {
            if (data.status === 422 && data.responseJSON) {
                console.log(data);
                if (Object.keys(data.responseJSON.errors).length) {
                    for (field in data.responseJSON.errors) {
                        let error = data.responseJSON.errors[field];
                        let input = '#Modal input[name=' + field + ']';
                        $(input).addClass('is-invalid');
                        $(input).next('span').find('strong').text(error[0]);
                    }
                }
            }
        })
     });

Laravel中的验证部件

代码语言:javascript
复制
$validator = Validator::make($request->all(),
        [
            'title' => 'required|string',
//            'description' => 'required|string',
//            'category' => 'required',
//            'county' => 'required',
            'startDate' => 'required',
            'endDate' => 'required',
//            'flexibility' => 'required',
            'contactName' => 'required',
            'contactEmail' => 'required',
            'contactNumber' => 'required|integer',
        ]);
        if ($validator->passes()) {
            return response()->json(['success'=>'Query successfully sent!'],200);
        }
        return response()->json(['error'=>$validator->errors()->all()],422);

在这个主题上也是如此。这是不是今天的标准所推荐的,用来解决使用modal时的验证。我知道使用Vue可能会让我在这里的生活更轻松,但我不想接触Vue,直到我对JavaScript有了基本的了解。

EN

回答 1

Stack Overflow用户

发布于 2019-10-04 15:52:02

我不知道这是否会有帮助,但通常这是我使用ajax请求的方式,我发现它容易多了,我希望这能有所帮助

代码语言:javascript
复制
   $.ajax({
            contentType: *the type your sending ex:'application/json'*,
            url: *Your url*,
            type: *POST or GET*,
            data: *Your Data ex: JSON.stringify(model) since i am sending json*,
            success: function (data) {
            },
            error: function (err) {
                console.error(err);
            }
        });
    },

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58231943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档