Laravel - Ajax

PHPz
發布: 2024-08-27 10:50:27
原創
660 人瀏覽過

Ajax (Asynchronous JavaScript and XML)is a set of web development techniques utilizing many web technologies used on the client-side to create asynchronous Web applications. Import jquery library in your view file to use ajax functions of jquery which will be used to send and receive data using ajax from the server. On the server side you can use the response() function to send response to client and to send response in JSON format you can chain the response function with json() function.

json() function syntax

json(string|array $data = array(), int $status = 200, array $headers = array(), int $options)
登入後複製

Example

Step 1− Create a view file calledresources/views/message.phpand copy the following code in that file.

  Ajax Example   
This message will be replaced using Ajax. Click the button to replace the message.
'getMessage()']); ?>
登入後複製

Step 2− Create a controller calledAjaxControllerby executing the following command.

php artisan make:controller AjaxController --plain
登入後複製

Step 3− After successful execution, you will receive the following output −

AjaxController

Step 4− Copy the following code in

app/Http/Controllers/AjaxController.phpfile.

app/Http/Controllers/AjaxController.php

json(array('msg'=> $msg), 200); } }
登入後複製

Step 5− Add the following lines inapp/Http/routes.php.

app/Http/routes.php

Route::get('ajax',function() { return view('message'); }); Route::post('/getmsg','AjaxController@index');
登入後複製

Step 6− Visit the following URL to test the Ajax functionality.

http://localhost:8000/ajax
登入後複製

Step 7− You will be redirected to a page where you will see a message as shown in the following image.

Replace Message

Step 8− The output will appear as shown in the following image after clicking the button.

Simple Message

以上是Laravel - Ajax的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!