Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过jQuery、Ajax和ASP.Net返回数据块的更好方法_Jquery_Asp.net_Ajax_Webmethod - Fatal编程技术网

通过jQuery、Ajax和ASP.Net返回数据块的更好方法

通过jQuery、Ajax和ASP.Net返回数据块的更好方法,jquery,asp.net,ajax,webmethod,Jquery,Asp.net,Ajax,Webmethod,我有一个ajax方法,可以根据页面上设置的条件检索数据,这可能是一些到很多记录。我将ajax调用改为一次轮询25条记录,而不是返回一个大数据集 我这样做的方式是在我的WebMethod上,根据条件运行sql,然后在第一次轮询时将行转换为JSON并保存到文件。后面的那些只是从文件加载回类,这样我就可以选择下一个25。我需要在第一次调用时运行一次数据,但我不能简单地选择25,因为函数需要加载所有记录以执行基于它的其他操作 我现在的问题是,对于一个抛出错误的报告,返回了一组非常大的数据:使用JSON

我有一个ajax方法,可以根据页面上设置的条件检索数据,这可能是一些到很多记录。我将ajax调用改为一次轮询25条记录,而不是返回一个大数据集

我这样做的方式是在我的WebMethod上,根据条件运行sql,然后在第一次轮询时将行转换为JSON并保存到文件。后面的那些只是从文件加载回类,这样我就可以选择下一个25。我需要在第一次调用时运行一次数据,但我不能简单地选择25,因为函数需要加载所有记录以执行基于它的其他操作

我现在的问题是,对于一个抛出错误的报告,返回了一组非常大的数据:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了maxJsonLength属性上设置的值

我已经设置了最大值,所以很明显我需要考虑另一种方法来保存首次运行的数据并将其分块发送回,有什么想法吗

编辑:
本质上,我正在寻找一种将类保存到文件并在后续请求中检索它的方法。

我已将JSON更改为正常序列化,如下所示:

BinaryFormatter bf = new BinaryFormatter();
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("path/data" + model.reportId + ".txt"), FileMode.Create);
bf.Serialize(fileStream, preview);
fileStream.Close();
以及反序列化:

BinaryFormatter bf = new BinaryFormatter();
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("path/data" + model.reportId + ".txt"), FileMode.Open);
fileStream.Seek(0, SeekOrigin.Begin);
DataPreview savedPreview = (DataPreview)bf.Deserialize(fileStream);
fileStream.Close();

在ASP.NET中,您可以访问all powerfull对象

您可以轻松创建记录集,然后将其添加到缓存中,如下所示:

HttpContext.Current.Cache.Insert("UniqueidentifierForObject", TheRecordSetObject, Nothing, DateTime.Now.AddHours(2), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, Nothing)
这将把对象插入缓存,绝对过期时间为2小时。如果你环顾四周,有很多关于缓存的好文档,所以我不打算在这里深入讨论。注意缓存的访问方式也很重要,因为在web方法或服务中不会有正常的
httpContext
,因此需要使用扩展引用

然后,在后续调用数据时,可以通过调用以下命令将对象从缓存中取出:

Dataset theDataset = HttpContext.Current.Cache("UniqueidentifierForObject");
if(theDataset == null){
    //CallYour load function because the cahce did not have your object
}else{
    //Do you pull for the chunk of data.
}
这样,您只需要执行一次数据库调用,但可以在需要时从集合中获取记录


另外,需要注意的是,ASP.NET在web方法和服务的序列化方面非常出色。这通常是为您而做的,尝试再次这样做可能会导致各种奇怪的行为。

查看asp.net Caching您是否有任何入门链接,因为有很多要查看。你能把它也作为答案贴出来吗