dongyilai4214 2016-09-29 06:35
浏览 109

Laravel 5.3 Ajax显示视图

I have a problem with Ajax. 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.

Below is file and code.

web.php

Route::get('/home', 'HomeController@nbp');
Route::post('/home','HomeController@nbp');

HomeController.php

    public function nbp(Request $request)
        {
                $data = $request->all();


            if($request->ajax() && $request->isMethod('post')){

                $data = response()->json($data);
                $data = $data->getContent();
                $data = json_decode($data, true);
                $currency = $data['currency'];
                Debugbar::info($currency);

            }else{
                $currency = 'EUR';
            }

             $tabeA = 'a';

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



                // Send a request
                $response = $client->request('GET', 'exchangerates/rates/'.$tableA.'/'.$currency);

                 $response->getStatusCode();
                // 200
                //$contentType = $response->getReasonPhrase();
                // 'application/json; charset=utf8'
                $currency =  json_decode($response->getBody());

                $data = $currency->rates;
                $data2 = $data[0]->mid;

       if($request->ajax() && $request->isMethod('post')){
             return view('home',compact('currency'))->render();
            }else{
    return view('home',compact('currency'));
            }

                }

script.js

$('#currencyNameA').change(function() {
    $.ajax({
    type:'post',
     dataType: 'json',
     headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
     url: '/home',
      data:  { currency : $('#currencyNameA').val()},
   success: function(data){


   },
   complete: function(data){


   }
  });
});

The request to API must be http://api.nbp.pl/api/$table/$currency

Example: http://api.nbp.pl/api/a/USD

  • 写回答

3条回答 默认 最新

  • du7999 2016-09-29 06:43
    关注

    Do the following:

    Return json from your controller if you do a ajax call:

    $currency =  json_decode($response->getBody());
    
                    $data = $currency->rates;
                    $data2 = $data[0]->mid;
    
           if($request->ajax() && $request->isMethod('post')){
                 return response()->json(["currency"=>$currency]);
                }else{
        return view('home',compact('currency'));
                }
    

    in the success function output the data to the page

     success: function(data){
        $('body').append(data.currency.rates[0].mid);//change the body element with your element
       },
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值