WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Laravel - Ajax


Laravel - Ajax : Ajax (Asynchronous JavaScript and XML) is a valuable it is set of set of web improvement strategies for the most part utilized on the customer side to make nonconcurrent Web applications.Import jquery library in your view record to utilize ajax elements of jquery which will be utilized to send and get information utilizing ajax from the server.

json() work syntax

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

Example

Step 1 − Create a view document called resources/sees/message.php and duplicate the accompanying code in that file.

<html> 

<head> 

<title>Laravel - Ajax</title> 

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> 

</script> 

<script> 

work getMessage(){ 

$.ajax({ 

type:'POST', 

url:'/getmsg', 

data:'_token = <?php reverberation csrf_token() ?>', 

success:function(data){ 

$("#msg").html(data.msg); 

} 

}); 

} 

</script> 

</head> 

<body> 

<div id = 'msg'>This message will be supplanted utilizing Laravel - Ajax. 

Snap the catch to supplant the message Laravel - Ajax.</div> 

<?php 

reverberation Form::button('Replace Message',['onClick'=>'getMessage()']); 

?> 

</body> 

</html> 

Step 2 − Create a controller called AjaxController by executing the accompanying command.

php craftsman make:controller AjaxController - plain 

Step 3 − After effective execution, you will get the accompanying yield −

AjaxController

Step 4 − Copy the accompanying code in

app/Http/Controllers/AjaxController.php file.

app/Http/Controllers/AjaxController.php

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 

use App\Http\Controllers\Controller; 

class AjaxController broadens Controller { 

open capacity index(){ 

$msg = "This is a basic message."; 

return reaction()- >json(array('msg'=> $msg), 200); 

} 

} 

Step 5 − Add the accompanying lines in app/Http/routes.php.

app/Http/routes.php

Route::get('ajax',function(){ 

return view('message'); 

}); 

Route::post('/getmsg','AjaxController@index'); 

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

http://localhost:8000/ajax 

Step 7 − You will be diverted to a page where you will see a message as appeared in the accompanying image.