title: Request of Ajax with Laravel 5.4
body: <p>I am trying to make an example of Ajax request with Laravel 5.4.</p>
  
  <p>The test example is simple, just enter a numeric value in an input = text field in my View and leave the field to send to the Controller, then add + 10 to that value and then return that value to my View so that it can displayed on an alert.</p>
  
  <p><strong>HTML</strong> : viewteste.blade.php</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action=""&gt;      
              Valor: &lt;input type="text" id="valor" name="valor" /&gt;
      &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>JS</strong>: The js file is inside viewteste.blade.php, I just split it to make it easier to interpret.</p>
  
  <pre><code>&lt;script&gt;
      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
  
      $(document).ready(function(){
         $("#valor").on('blur', function()
         {
              valor = $(this).val();    
              $.ajax({
  
                  type:'POST',
                  url:"{{ URL::to('/teste/valor') }}",
                  dataType: 'JSON',
                  data: {
                      "valor": valor
                  },
                  success:function(data){
                      alert('Success');
                  },
                  error:function(){
                    alert('Error');
                  },
              });
  
  
         });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Route</strong></p>
  
  <pre><code>Route::get('/teste',      'TesteAjaxController@index');
  Route::post('/teste/valor', 'TesteAjaxController@valor');
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>class TesteAjaxController extends Controller
  {
       public function index()
      {
          return view('painel.viewteste');
      }
  
      public function valor(Request $request)
      {
          $valor= $request-&gt;input('valor');
          $valor += 10;
          return $valor; // How do I return in json? in case of an error message?
  
      }
  }
  </code></pre>
  
  <p>Always when I try to send the request via ajax it goes to alert ('Error'). is it that I'm doing something wrong in sending the ajax or route?</p>