skip to Main Content

Im using OctoberCMS, the user plugin and I want to send data via AJAX to a controller and save the data in the database (in the column of the logged in user).

So I created a new Route in my routes.php

<?php
Route::get('saveHighscore', 'testProfileControllersHighScore@saveHighscore')
->middleware('web');

And a controller

<?php
namespace TestProfileControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
use OctoberRainAuthModelsUser;
use RainLabUserFacadesAuth;

class HighScore extends IlluminateRoutingController
{
function saveHighscore(Request $request) {
    DB::table('users')->where(['id' => Auth::getUser()->id])->update(['highscore' => $request]);
}
}

And my jQuery calls

$.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: "/saveHighscore",
        type: "POST",
        data: highscore
    });

But nothing works. If I call /saveHighscore in my browser with dummy data in the controller, it works fine

2

Answers


  1. The AJAX framework only works on the CMS controller or Backend controllers (controllers extending backend/classes/controller). If you’re wanting to send data via AJAX without using the built in AJAX framework, then we would have to see more information from your console / network tab of your browser dev tools to see why exactly it’s failing.

    Login or Signup to reply.
  2. It should work without any issue.

    But I think you are making 2 different requests

    In ajax config you specified -> type: "POST" and you are listening for get request

    May be you just need to change Route::get -> Route::post

    Now it should work expected.

    If any doubts please comment.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search