Laravel 5.3从api中更改请求

时间:2016-09-29 09:19:43

标签: php ajax laravel

我遇到了Ajax的问题​​,我不知道如何处理它。我有一个控制器,从外部API得到响应。在此控制器中将变量传递给API请求。结果传递给视图。在这个视图中,我有货币的下拉列表。我想当用户选择另一种货币时,新请求将发送到API并生成新视图。

下面是文件和代码。

web.php

Route::get('/currency', 'HomeController@getCurrency');
Route::post('/currency', 'HomeController@getCurrency');

我有一个观点:

@extends('layouts.app') @section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Currency</div>

                <div class="panel-body">

                {!! Form::open(array('url' => '/currency','method' =>'post')) !!}
                {!! csrf_field() !!}
                <select name="currency" id="currencyNameA">
                 @foreach($currency_table_A->rates as $rows)

                 <option value="{{ $rows->code }}">{{ $rows->currency }}</option>

                @endforeach
                </select>
                {!! Form::close() !!}

                    <br />

                <h2><div class="currency">
                currency {{$currency->currency}} {{ $currency->rates[0]->mid}} last update was {{ $currency->rates[0]->effectiveDate }}
                </div>
                </h2>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

还有一个控制器:

 public function getCurrency(Request $request)
     {       

          $client = new GuzzleHttpClient(['base_uri' => 'http://api.nbp.pl/api/'],['headers'=>['content-type'=>
 'application/json']]);

             //Table for dropdown list Begin
             $table_response = $client->request('GET','exchangerates/tables/a');
             $currency_table_A =  json_decode($table_response->getBody());
             $currency_table_A = $currency_table_A[0];
             //Table for dropdown list End

         if($request->ajax() && $request->isMethod('post')){
             $cur = $request->input('currency'); //get currency from post
         }else{
             $cur = 'EUR';
         }  

     // Send a request to External API
     $response = $client->request('GET', 'exchangerates/rates/a/'.$cur);

     //Antwser from External API
     $currency =  json_decode($response->getBody());


   if($request->ajax() && $request->isMethod('post')){
        //In Debug bar i get reply form API but my View is not change
        Debugbar::info($currency);
        return response()->json(view('curentCurrency', compact('currency','currency_table_A'))->render());
    }else{
        return view('curentCurrency', compact('currency','currency_table_A'));
    }
    }

的script.js

$('#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){  
   }
  });
});

我需要在下拉列表中选择新货币时将新答案传递给View。当第一次加载页面时,当有人从下拉列表中更改时,我需要获得美元的基本值,因此该用户选择的货币结果应该会更改。

0 个答案:

没有答案