0

I am working with laravel and ajax. But when I register I see this error 302.

I know this may be a trivial question but I am just not able to get this ajax call to work.

Error 302

Auth/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']
    ]);
}

My ajax is register.js file

How do I pass the "Accept'=>'application/json" in request when testing:

I want to add 'accept'=>'application/json' to my request header.

$.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');
            }
        });
    });

});
Madhuri Patel
  • 1,270
  • 12
  • 24

2 Answers2

0

Update your ajax header, according to this:

$.ajaxSetup({
    headers: {
        accepts: "application/json; charset=utf-8",
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
train_fox
  • 1,517
  • 1
  • 12
  • 31
  • Sorty. I edited it. Also you can refer to provided link. – train_fox May 25 '18 at 08:52
  • What laravel version you are using? I did same code in laravel 5.6. Maybe return json response is not implemented on previous versions. I suggest you on register controller check if $request->isAjax() then return object model. – train_fox May 25 '18 at 09:01
  • Try to return ajax yourself. As i said before you can check if $request->isAjax() and then return created user model from register function. – train_fox May 25 '18 at 09:10
0

The possibilities of this issue is CSRF token and missing route.

Rajesh kumar
  • 188
  • 7