json(string|array $data = array(), int $status = 200, array $headers = array(), int $options)
<html>
<head>
<title>laravel ajax示例</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script>
function getmessage(){
$.ajax({
type:'get',
url:'/getmsg',
data:'_token = <?php echo csrf_token() ?>',
success:function(data){
$("#msg").html(data.msg);
}
});
}
</script>
</head>
<body>
<div id = 'msg'>这条消息将会使用ajax来替换.
点击下面的按钮来替换此消息.</div>
<?php
echo form::button('替换消息',['onclick'=>'getmessage()']);
?>
</body>
</html>
php artisan make:controller ajaxcontroller
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 extends controller {
public function index(){
$msg = "这是一条简单的消息.";
return response()->json(array('msg'=> $msg), 200);
}
}
app/http/routes.php
route::get('ajax',function(){
return view('message');
});
route::get('/getmsg','ajaxcontroller@index');
http://localhost:8000/ajax


