Php 将localstorage与ajax合并

Php 将localstorage与ajax合并,php,ajax,Php,Ajax,我将如何合并这两位代码,有人能解释键和值是什么吗。 我正在构建一个通知系统,我希望存储最后一个新的通知id,但如果它是同一个通知id,就不要将其反复插入div,因此ajax会在我的服务器中搜索任何可能是新的内容 阿贾克斯 函数loadIt(){ var通知_id=“” $.ajax({ 键入:“获取”, url:“viewajax.php?notification_id=“+notification_id, 数据类型:“json”, cache:false, 成功:函数(dataHandler

我将如何合并这两位代码,有人能解释键和值是什么吗。 我正在构建一个通知系统,我希望存储最后一个新的通知id,但如果它是同一个通知id,就不要将其反复插入div,因此ajax会在我的服务器中搜索任何可能是新的内容

阿贾克斯


函数loadIt(){
var通知_id=“”
$.ajax({
键入:“获取”,
url:“viewajax.php?notification_id=“+notification_id,
数据类型:“json”,
cache:false,
成功:函数(dataHandler){
}
});
}
设置间隔(加载时间,10000);
本地存储

window.localStorage.setItem('key', 'value');
var dataHandler = function (response){
   var isDuplicate = false, storedData = window.localStorage.getItem ('key');

   for (var i = 0; i < storedData.length; i++) {
     if(storedData[i].indexOf(response) > -1){
        isDuplicate = true;
     }
   }
   if(!isDuplicate){
     storedData.push(response);
   }
}; 

var printer = function(response){
   if(response.num){
      $("#notif_actual_text-"+notification_id).prepend('<div  id="notif_actual_text-'+response['notification_id']+'" class="notif_actual_text"><a href="'+response['notification_id']+'">'+response['notification_content']+' </a><br />'+response['notification_time']+'</div></nr>');

      $("#mes").html(''+ response.num + '');
    }
};
window.localStorage.setItem('key','value');
var dataHandler=函数(响应){
var isDuplicate=false,storedData=window.localStorage.getItem('key');
对于(变量i=0;i-1){
isDuplicate=真;
}
}
如果(!isDuplicate){
storedData.push(响应);
}
}; 
变量打印机=功能(响应){
if(response.num){
$(“#notif_实际_文本-”+通知id)。前置(“
”+响应['notification_time']+”); $(“#mes”).html(“”+response.num+“”); } };
您已经手动将老式的Ajax与jQuery混淆了。jQuery中
success
函数的参数不是函数名或处理程序。它是一个变量名,将包含来自服务器的响应。success函数本身相当于以旧方法创建的处理函数

所以不是:

 success: function(dataHandler){ }
 ...
 ...
 var dataHandler = function (response){
而是:

 success: function(response) { doCallsToSaveToLocalStorage(response); }

您需要将localStorage属性(即atring)转换为对象,以使用像这样方便的子属性。JSON.parse/stringify适用于许多数据形状。为了防止重复,可以在追加之前查找现有的id匹配(#notif_actual_text-“+notification_id),如果dom中有一个id,则可以提前中止。
 success: function(response) { doCallsToSaveToLocalStorage(response); }