1

I am having a problem with Laravel.

I try to do a POST with JQuery to my Controller, but when I do that, I recieve a HTTP 302 code (found), and after that It creates a GET to the same controller URL.

Here there is my code:

WizardController.php

class WizardController extends Controller
{
    public function saveForm(Request $request) {
        $data = $request->data;
        error_log(print_r($data, true));
        return response()->json(['message' => 'test']);
    }
}

routes/web.php

Route::group(['prefix' => 'wizard', 'middleware' => 'auth'], function(){
    Route::post('create', 'WizardController@saveForm');
})

Wizard.html

<script>
    var token = $('meta[name=csrf-token]').attr('content');
    $.ajax('wizard/create', function() {
        method: 'POST',
        data:    {'data' : 'test'},
        headers: { 'X-CSRF-TOKEN': token },
        success: function(data) {
            console.log(data);
        }
     });
</script>

I even cannot see the log in my controller. Any suggestion?

Thanks

string_not_found
  • 23
  • 1
  • 1
  • 6

2 Answers2

3

You have middleware 'auth' for your route. You should investigate a bit on it and you'll understand why doesn't it work.

The point is that 'auth' middleware requires cookies to work correctly, while you have it empty when you're have Ajax request. Why does it give you 302 and not 401? You should look into your authentication handler for any custom logic on that.

What to do? Implement JWT auth or stateless authentication!

1

OK problem solved...

My problem was in routes/web.php, I declared that route inside a Route group, which has two other customized middlewares.

One of those middleware was making me that wrong redirection.

string_not_found
  • 23
  • 1
  • 1
  • 6