同期的にやるなら次のような感じでは無いでしょうか?
lang
1$(function() {
2 console.log(get_hoge());
3 function get_hoge() {
4 var result;
5 $.ajax({
6 url: 'hoge.php',
7 async: false,
8 type:'POST',
9 dataType: 'json',
10 cache : false,
11 data : {piyo : '1'}
12 }).done(function(data) {
13 result = data.hoge;
14 }).fail(function() {
15 console.log("error");
16 });
17 return result;
18 }
19});
非同期のままにするなら $.ajax の戻り値をそのまま返して呼び出し元で then とか done とかをするか、
lang
1$(function() {
2 get_hoge().then(function(data){
3 console.log(data.hoge);
4 });
5 function get_hoge() {
6 return $.ajax({
7 url: 'hoge.php',
8 type:'POST',
9 dataType: 'json',
10 cache : false,
11 data : {piyo : '1'}
12 });
13 }
14});
コールバックを渡してやることになると思います。
lang
1$(function() {
2 get_hoge(function(hoge){
3 console.log(hoge);
4 });
5
6 function get_hoge(callback) {
7 $.ajax({
8 url: 'hoge.php',
9 type:'POST',
10 dataType: 'json',
11 cache : false,
12 data : {piyo : '1'}
13 }).done(function(data) {
14 callback(data.hoge);
15 }).fail(function() {
16 console.log("error");
17 });
18 }
19});