title: Laravel 5.3 change request from api in view
body: <p>I have a problem with Ajax and I don't know how to deal with it. I have a controller where is get response from external API. In this controller are passing variables to API request. The result is passing to a view. On this view i have drop down list with currency's. I want when user select another currency the new request will be send to API and generate new view.</p>
  
  <p>Below is file and code.</p>
  
  <p>web.php</p>
  
  <pre><code>Route::get('/currency', 'HomeController@getCurrency');
  Route::post('/currency', 'HomeController@getCurrency');
  </code></pre>
  
  <p>I have a view:</p>
  
  <pre><code>@extends('layouts.app') @section('content')
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-8 col-md-offset-2"&gt;
              &lt;div class="panel panel-default"&gt;
                  &lt;div class="panel-heading"&gt;Currency&lt;/div&gt;
  
                  &lt;div class="panel-body"&gt;
  
                  {!! Form::open(array('url' =&gt; '/currency','method' =&gt;'post')) !!}
                  {!! csrf_field() !!}
                  &lt;select name="currency" id="currencyNameA"&gt;
                   @foreach($currency_table_A-&gt;rates as $rows)
  
                   &lt;option value="{{ $rows-&gt;code }}"&gt;{{ $rows-&gt;currency }}&lt;/option&gt;
  
                  @endforeach
                  &lt;/select&gt;
                  {!! Form::close() !!}
  
                      &lt;br /&gt;
  
                  &lt;h2&gt;&lt;div class="currency"&gt;
                  currency {{$currency-&gt;currency}} {{ $currency-&gt;rates[0]-&gt;mid}} last update was {{ $currency-&gt;rates[0]-&gt;effectiveDate }}
                  &lt;/div&gt;
                  &lt;/h2&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>And a controller:</p>
  
  <pre><code> public function getCurrency(Request $request)
       {       
  
            $client = new GuzzleHttpClient(['base_uri' =&gt; 'http://api.nbp.pl/api/'],['headers'=&gt;['content-type'=&gt;
   'application/json']]);
  
               //Table for dropdown list Begin
               $table_response = $client-&gt;request('GET','exchangerates/tables/a');
               $currency_table_A =  json_decode($table_response-&gt;getBody());
               $currency_table_A = $currency_table_A[0];
               //Table for dropdown list End
  
           if($request-&gt;ajax() &amp;&amp; $request-&gt;isMethod('post')){
               $cur = $request-&gt;input('currency'); //get currency from post
           }else{
               $cur = 'EUR';
           }  
  
       // Send a request to External API
       $response = $client-&gt;request('GET', 'exchangerates/rates/a/'.$cur);
  
       //Antwser from External API
       $currency =  json_decode($response-&gt;getBody());
  
  
     if($request-&gt;ajax() &amp;&amp; $request-&gt;isMethod('post')){
          //In Debug bar i get reply form API but my View is not change
          Debugbar::info($currency);
          return response()-&gt;json(view('curentCurrency', compact('currency','currency_table_A'))-&gt;render());
      }else{
          return view('curentCurrency', compact('currency','currency_table_A'));
      }
      }
  </code></pre>
  
  <p>Script.js</p>
  
  <pre><code>$('#currencyNameA').change(function() {
      $.ajax({
      type:'post',
       dataType: 'json',
       headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
       url: '/currency',
        data:  { currency : $('#currencyNameA').val()},
     success: function(data){  
     }
    });
  });
  </code></pre>
  
  <p>I need to pass a new answer to a View when was select new currency in drop list. When page is loaded first time i need to get basic value in USD when someone change from drop down list the currency result should change for that user choose.</p>