黄色电影一区二区,韩国少妇自慰A片免费看,精品人妻少妇一级毛片免费蜜桃AV按摩师 ,超碰 香蕉

Laravel Ajax

laravel ajax

ajax(異步j(luò)avascript和xml) 是一組利用客戶端使用的許多web技術(shù)創(chuàng)建異步web應(yīng)用程序的web開發(fā)技術(shù)。在你的視圖文件中導入jquery庫以使用jquery的ajax函數(shù),這些函數(shù)將用于使用服務(wù)器的ajax發(fā)送和接收數(shù)據(jù)。在服務(wù)器端,您可以使用response()函數(shù)向客戶端發(fā)送響應(yīng),并以json格式發(fā)送響應(yīng),您可以使用json()函數(shù)鏈接響應(yīng)函數(shù)。

 

json()函數(shù)的語法

json(string|array $data = array(), int $status = 200, array $headers = array(), int $options)

第1步 - 創(chuàng)建一個名為 resources / views / message.php 的視圖文件,并將該代碼復(fù)制到該文件中。

    
      <title>ajax example</title>       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script>
         function getmessage(){
            $.ajax({
               type:'post',
               url:'/getmsg',
               data:'_token = <?php echo csrf_token() ?>',
               success:function(data){
                  $("#msg").html(data.msg);
               }
            });
         }
      </script>
   

   
      
this message will be replaced using ajax. click the button to replace the message.
echo form::button('replace message',['onclick'=-->'getmessage()']); ?>

第2步 - 通過執(zhí)行以下命令創(chuàng)建一個名為 ajaxcontroller 的控制器。

php artisan make:controller ajaxcontroller --plain

第3步 - 成功執(zhí)行后,您將收到以下輸出 -

ajaxcontroller

第4步 - 復(fù)制下面的代碼

app / http / controllers / ajaxcontroller.php 文件。

應(yīng)用程序/ http /控制器/ ajaxcontroller.php


namespace app\http\controllers;

use illuminate\http\request;
use app\http\requests;
use app\http\controllers\controller;

class ajaxcontroller extends controller {
   public function index(){
      $msg = "this is a simple message.";
      return response()--->json(array('msg'=> $msg), 200);
   }
}

第5步 - 在 app / http / routes.php中 添加以下行。

應(yīng)用程序/ http / routes.php文件

route::get('ajax',function(){
   return view('message');
});
route::post('/getmsg','ajaxcontroller@index');

第6步 - 訪問以下url以測試ajax功能。

http://localhost:8000/ajax

第7步 - 您將被重定向到一個頁面,您將看到一條消息,如下圖所示。

第8步 - 點擊按鈕后,輸出將如下圖所示。

下一節(jié):laravel 錯誤處理

laravel 教程

相關(guān)文章