1

I'm trying to set axios request but always getting missing argument, but not sure how can I pass it can someone explain why this wont work?

my route: (web.php)

Route::post('/api/setSuccessMessage', 'API\SessionsController@setSuccessMessage');

my controller: (Controllers/API/SessionsController.php)

class SessionsController extends Controller
{
    public static function setSuccessMessage($key, $value)
    {
        session()->put($key ,$value);
    }...

and my vueJS axios call (resources/assets/components/Login.vue)

created: function () {
            // check is user logged out
            axios.post('/api/setSuccessMessages', {message: 'message',message2: 'message2'}).then((response)=> {
                console.log(response);
            })
        },
4
  • Can you give us the names of the files the codes are in instead of my route and such? Commented Feb 9, 2018 at 9:25
  • @NikolaGavric updated Commented Feb 9, 2018 at 9:31
  • Are you using laravel built-in authentication? Commented Feb 9, 2018 at 9:37
  • @NikolaGavric no, something custom, but I just need set session message for now just to see how can I pass arguments with axios Commented Feb 9, 2018 at 9:38

2 Answers 2

4

Use :

 public static function setSuccessMessage(Request $request)
 {
     $key = $request->get('message');
     $value = $request->get('message2');

     session()->put($key ,$value);
 }
Sign up to request clarification or add additional context in comments.

Comments

4

If you want to send the data as a part of your request body you can do so like

axios.post('/api/setSuccessMessages', {message: 'message',message2: 'message2'})
     .then((response)=> {
           console.log(response);
      }
)
//you then accept them as such
Route::post('/api/setSuccessMessage/{message}/{message2}', 'API\SessionsController@setSuccessMessage');

If you wish to send the data as a request params (everything after ? in the url) you can do so like this

 var params = {
    message1: message1,
    message2: message2
 };
 axios.post('/api/setSuccessMessages', {}, {'params': params})
     .then((response)=> {
           console.log(response);
      }
)
//you then accept them as such
Route::post('/api/setSuccessMessage', 'API\SessionsController@setSuccessMessage');
//You can further use them as such in controller
function public test(Request $request) {
   $request->get('message1');
   $request->get('message2');
}

I'll reference axios official docs for the axios request params

1 Comment

nice solution, this second part is more of what I needed, check vpalade solution its bit cleaner for same result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.