繁体   English   中英

Laravel:没有数据通过Ajax发送

[英]Laravel: no data being sent via ajax

我制作了一个自定义的Jquery插件,以帮助我轻松地通过Ajax将数据发送到针对laravel量身定制的服务器,但是显然我无法发送任何数据。 当我使用dd($ request-> all())检查已发送到laravel服务器的内容时​​,在控制台中收到一个空数组,表示我没有发送任何内容。 下面是我的代码

JS

(function($){
    'use strict'

    $.fn.lajax=function(options){


        //overwrite options
        var optns=$.extend({
            dataType: 'json',
            debug:false,
            processData: true,
            headers:{
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            acceptedInputFile:'/*/',

            //functions that are very similar to the ajax optns but differ a little 
            //as the paramters give easy access to the ajax form element
            lajaxBeforeSend: function(form,formData,jqXHR,settings){},
            lajaxSuccess: function(form,formData,data,textStatus,jqXHR){},
            lajaxError: function(form,formData,jqXHR,textStatus,errorThrown){},
        },options);



        //loop through every form, 'this' refers to the jquery object (which should be a list of from elements)
        this.each(function(index,el){

            $(el).submit(function(e){
                //prevent submission
                e.preventDefault();

                //form jquery instance & form data
                var $form=$(this);
                var formData = new FormData($form[0]);

                //catch url where the ajax function is supposed to go
                var url=$form.attr('action');

                //check if REST based method is assigned
                if($form.find('input[name="_method"]').length)
                {
                    var method=$(this).find(':input[name="_method"]').val().toUpperCase();
                    if(optns.debug)
                        console.log("'"+method+"' method registered for form submission");
                }
                //If no REST method is assigned, then check method attr
                else if($form.attr('method'))
                {
                    var method=$form.attr('method').toUpperCase();
                    if(optns.debug)
                        console.log("'"+method+"' method registered for form submission");
                }
                //method is not assigned
                else
                {
                    var method='GET';
                    if(optns.debug)
                        console.log('The form that submitted has no method type assigned. GET method will be assigned as default');
                }



                //object that will be fed into jquerys ajax method
                var ajax_options={
                    url: url,
                    method: method,
                    beforeSend: function(jqXHR,settings){
                        console.log(jqXHR);
                        console.log(settings);
                        if(optns.debug)
                            console.log('executing beforeSend function');
                        optns.lajaxBeforeSend($form,formData,jqXHR,settings);
                    },
                    success: function(data,textStatus,jqXHR){
                        if(optns.debug)
                            console.log('executing success function');
                        optns.lajaxSuccess($form,formData,data,textStatus,jqXHR)
                    },
                    error: function(jqXHR,textStatus,errorThrown){
                        if(optns.debug)
                            console.log('error encountered. ajax error function procked');
                        optns.lajaxError($form,formData,jqXHR,textStatus,errorThrown);

                        var errors = jqXHR.responseJSON;
                        console.log(errors);
                    },
                }

                //check if files are included in the submitted form if the method is not GET
                if($form.find('input:file').length && method!='GET'){
                    ajax_options.processData=false;
                    ajax_options.contentType=false;
                    ajax_options.cache=false;
                    ajax_options.data=formData;
                }


                if(optns.debug)
                    console.log('About to send ajax request');

                //sending request here
                $.ajax(ajax_options);

                if(optns.debug)
                    console.log('finished with form '+$form+'. Looping to next form if exists');

                return false;
            });

        });

        return this;
    }

}(jQuery));

的HTML

<form class="lajax" action="{{ action('AlbumController@store') }}" method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <label>Album Name</label>
                    <input type="text" name="name" class="form-control">                                                
                </div>

                <div class="form-group">
                    <label for="coverFile">Album Cover Image</label>
                    <input  name="cover" type="file" id="coverFile">
                    <p class="help-block">Example block-level help text here.</p>
                </div>

                <div class="form-group">
                    <label for="albumFiles">Album Images</label>
                    <input type="file" name="photos[]" multiple>
                </div>

                <button type="submit" class="btn btn-primary">Create Album</button>
            </form>

我认为我的JS没有将我的数据发送到服务器,但是我不确定为什么。 请帮忙

您是否在路线文件中定义了一个精确指向AlbumController@store Controller方法的发布路线

暂无
暂无

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

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