getJSON メソッドは非同期であるためです。何が起こっていますか...
getInviteLink : function() { // this function is called...
var me = this,
invite_url; // happens right away
$.getJSON('get_invite_code.json', function(data) {
// Step 2, these statements don't happen until later
// -- when the response is received
console.log(data.invite_url); // this
invite_url = data.invite_url;
}); // Step 1. the getJSON is called right away
console.log(invite_url) // this happens right after the
// the getJSON is called, but before
// it gets a response. Thus no data when the log
// statement is invoked
return invite_url; // also no data (yet) when this return statement
// is executed
},
追加されinvite_url
たソフトウェアを修正するには、JSON 応答がサーバーから受信されるまで、何もできないことを認識する必要があります。-- また、クライアントがネットワークやサーバーに問題を抱えている場合、受信できない可能性があります。
関数を getJSON 呼び出しに渡すことができます。この関数は、invite_url を利用します。それまでの間、クライアントのために他のことを行うことができます。
応答を受け取るまでソフトウェアが何もすることがない場合は、ビジー インジケーター (回転する円など) を設定し、json 要求を起動して、応答が返ってきたときに関数がビジー インジケーターをキャンセルするようにします。
詳細については、jQuery json ドキュメントを参照してください。