0

I have a textarea and I try to submit data with axios and vue to laravel but it doesn't work.I try to take the data and put ucfirst then return it.

new Vue({
el:'#root',
data:{

  areamodel: ''
},

methods:{
 insert:function(){
     axios.post('/vue').then(response => this.areamodel = response.data);
   }

}

});

and

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
       public function vue(Request $request)
    {
$abc = ucfirst($request->input('areamodel'));

return $abc;


    }
}
5
  • What is the error displayed? Commented Aug 13, 2017 at 6:01
  • just blank no error when i go to vue dev tools it just the areamodel: "" Commented Aug 13, 2017 at 6:06
  • try console.log(response.data); Commented Aug 13, 2017 at 6:13
  • in dev tool now it shows areamodel:undefined Commented Aug 13, 2017 at 6:18
  • I have posted a solution Commented Aug 13, 2017 at 6:25

1 Answer 1

3

The problem is that the instance of vue is not available inside the axios.

new Vue({
el:'#root',
data:{
  areamodel: ''
},

methods:{
 insert:function(){
     let vueInstance = this; //This line is important

     axios.post('/vue', {areamodel: vueInstance.areamodel}).then(
       function(response){
         vueInstance.areamodel = response.data.areamodel;
       }
     ).catch(function(error){ console.log(error.message); });
   }
}

});

And server side

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
   public function vue(Request $request)
   {
     $abc = ucfirst($request->input('areamodel'));

     return response()->json(['areamodel' => $abc]);

   }
}
Sign up to request clarification or add additional context in comments.

7 Comments

same, i think it has something in laravel
i put this return ['areamodel']; and now it work but it just areamodel back, i need the output of the areamodel
it returns [object Object]
Did you replace response.data with response.data.areamodel
it returns [object Object] right? So in console you can expand the object in network tab in dev tools. Can you try it and confirm if response is correct?
|

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.