Ajax与laravel Post返回

时间:2018-05-25 07:56:03

标签: ajax laravel

我正在使用laravel和ajax。但是当我注册时,我看到了这个错误302.

我知道这可能是一个微不足道的问题,但我无法让这个ajax调用工作。

Error 302

认证/ RegisterController.php

protected function create(array $data, Request $request)
{
    if ($request->hasFile('image')) {
        $fileNameWithExt = $request->file('image')->getClientOriginalName();
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        $extention = $request->file('image')->getClientOriginalExtension();
        $fileNameToStore = $filename.'_'.time().'.'.$extention;
        $path = $request->file('image')->storeAs('public/images', $fileNameToStore);
    } else {
        $fileNameToStore = 'noimage.jpg';
    }

    return User::create([
        'firstname' => $data['firstـname'],
        'lastname' => $data['lastـname'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'mobile' => $data['mobile'],
        'nasional_code' => $data['national_code'],
        'birthdate' => $data['birthـdate'],
        'document' => $data['document'],
        'educational' => $data['educational'],
        'gender' => $data['gender'],
        'side' => $data['side'],
        $fileNameToStore => $data['image']
    ]);
}

我的ajax是register.js文件

测试时如何在请求中传递“Accept'=>'application / json”:

我想将'accept'=>'application / json'添加到我的请求标题中。

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

function scroll_to_class(element_class, removed_height) {
    var scroll_to = $(element_class).offset().top - removed_height;
    if($(window).scrollTop() != scroll_to) {
        $('html, body').stop().animate({scrollTop: scroll_to}, 0);
    }
}
function bar_progress(progress_line_object, direction) {
    var number_of_steps = progress_line_object.data('number-of-steps');
    var now_value = progress_line_object.data('now-value');
    var new_value = 0;
    if(direction == 'right') {
        new_value = now_value + ( 100 / number_of_steps );
    }
    else if(direction == 'left') {
        new_value = now_value - ( 100 / number_of_steps );
    }
    progress_line_object.attr('style', 'width: ' + new_value + '%;').data('now-value', new_value);
}
jQuery(document).ready(function() {

    $('form fieldset:first').fadeIn('slow');

    $('form input[type="text"], form input[type="password"], form textarea').on('focus', function() {
        $(this).removeClass('input-error');
    });

    $('form .btn-next').on('click', function() {
        var parent_fieldset = $(this).parents('fieldset');
        var next_step = true;
        var current_active_step = $(this).parents('form').find('.form-wizard.active');
        var progress_line = $(this).parents('form').find('.progress-line');

        parent_fieldset.find('input[type="text"], input[type="password"], input[type="email"], input[type="radio"]').each(function() {
            if( $(this).val() == "" ) {
                $(this).addClass('input-error');
                next_step = false;
            }
            else {
                $(this).removeClass('input-error');
            }
        });

        parent_fieldset.find('input[type="checkbox"]').each(function() {
            if( $(this).prop("checked") == false ) {
                $('.form-check-label').css("color","red");
                next_step = false;
            }
            else {
                $('.form-check-label').css("color","black");
            }
        });

        if( next_step ) {
            parent_fieldset.fadeOut(400, function() {
                current_active_step.removeClass('active').addClass('activated').next().addClass('active');
                bar_progress(progress_line, 'right');
                $(this).next().fadeIn();
                scroll_to_class( $('form'), 20 );
            });
        }

    });

    // previous step
    $('form .btn-previous').on('click', function() {
        var current_active_step = $(this).parents('form').find('.form-wizard.active');
        var progress_line = $(this).parents('form').find('.progress-line');

        $(this).parents('fieldset').fadeOut(400, function() {
            current_active_step.removeClass('active').prev().removeClass('activated').addClass('active');
            bar_progress(progress_line, 'left');
            $(this).prev().fadeIn();
            scroll_to_class( $('form'), 20 );
        });
    });

    $('form').on('submit', function(e) {
        $(this).find('input[type="text"], input[type="password"], input[type="username"], input[type="email"], input[type="tel"], input[type="url"], textarea').each(function() {
            if( $(this).val() == "" ) {
                e.preventDefault();
                $(this).addClass('input-error');
            }
            else {
                $(this).removeClass('input-error');
            }
        });
    });

});

2 个答案:

答案 0 :(得分:0)

根据this更新您的ajax标头:

$.ajaxSetup({
    headers: {
        accepts: "application/json; charset=utf-8",
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

答案 1 :(得分:0)

此问题的可能性是CSRF令牌和缺少路线。