別のサーバーでホストされている json ファイルを検索する javascript/ajax 関数があります。次のアクションを実行するには、関数が必要です。
- 外部サーバーからjsonファイルを取得する
- 独自のローカル サーバーに json ファイルを保存する
- json ファイルが 1 時間以上経過しているかどうかを確認する
- 1 時間以上経過していない場合は、そのデータを使用します
- 1 時間以上経過している場合 - 外部サーバーから再ダウンロードし、ローカル バージョンを上書きします
現在、私の関数は、呼び出されるたびに外部サーバーからデータを取得します。このキャッシング機能を追加する必要がありますが、その方法がわかりません。
誰でもアドバイスを提供できますか?
ここに私のコード:
function ajaxLoad(page,url,type,variety,category , widgetClickedId)
{
/*
* Checking the clicked id is the same as passed in one
* TODO refactor so the clicked id only ever gets passed
*/
if(widgetId != widgetClickedId && widgetClickedId != undefined)
{
return false;
}
var website = $('#button-print'+widgetId).data("website");
var page = $('#button-print'+widgetId).data("page");
$.ajax({
type: 'GET',
url: 'www.myothersite.com/api/v1/productchoice.json?website='+website,
async: true,
jsonp: 'callback',
dataType: 'jsonp',
success: function(productchoice)
{
if(productchoice['brand'+widgetId] == 'null' || productchoice['brand'+widgetId] == undefined)
{
productchoice['brand'+widgetId] = '';
}
//check that all values are not null , if not , then show the widget
if( productchoice['brand'+widgetId] == ''
&& productchoice['subject'+widgetId] == ''
&& productchoice['market'+widgetId] == ''
&& productchoice['type'+widgetId] == ''
&& productchoice['bookazinebrands'+widgetId] == '')
{
//is this corect?
$('#gdmContainer'+widgetId).hide();
//return false;
}
else
{
$('#gdmContainer'+widgetId).show();
}
setRibbons(productchoice.ribbonTop , productchoice.ribbonBottom);
getProductChoice( productchoice );
setLoveTitle( productchoice);
setDefaultBtn(productchoice['defaultBtn'+widgetId]);
return productchoice;
}
});
前もって感謝します!