Skip to content

Instantly share code, notes, and snippets.

@christurnertv
Created April 11, 2014 19:09
Show Gist options
  • Save christurnertv/10493201 to your computer and use it in GitHub Desktop.
Save christurnertv/10493201 to your computer and use it in GitHub Desktop.
jQuery Ajax and Laravel Demo
@extends('layouts.master')
@section('content')
<button id="btnGet" class="btn">Ajax Get</button>
<button id="btnPost" class="btn">Ajax Post</button>
@stop
@section('bottomscript')
<script>
$('#btnGet').on('click', function () {
var params = {
'key1': 'value1',
'key2': 'value2'
};
$.get("/ajax/get", params, successCallback);
alert('hello');
// $.ajax({
// url: "/ajax/get",
// data: params,
// success: successCallback,
// dataType: "json"
// });
});
$('#btnPost').on('click', function () {
var params = {
'key1': 'value1',
'key2': 'value2'
};
$.post("/ajax/post", params, successCallback);
// $.ajax({
// type: "POST",
// url: "/ajax/post",
// data: params,
// success: successCallback,
// dataType: "json"
// });
});
function successCallback(data) {
//alert(data.message);
console.log('success:');
console.log(data);
}
</script>
@stop
Route::get('/ajax/get', function()
{
Log::info("Received get.");
Log::info(Input::all());
$reply = array('x' => 1, 'error' => false, 'message' => 'Here is a message from the server.');
return Response::json($reply);
});
Route::post('/ajax/post', function()
{
Log::info("Received post.");
Log::info(Input::all());
$reply = array('error' => false, 'message' => 'Processed post.');
return Response::json($reply);
});
Route::get('/', function()
{
return View::make('ajax');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment