Created
April 11, 2014 19:09
-
-
Save christurnertv/10493201 to your computer and use it in GitHub Desktop.
jQuery Ajax and Laravel Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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