我如何在两个Ajax请求之间共享数据 [英] How do i share data between two Ajax Request
问题描述
嗨
我在WebService中创建了两个函数,并且正在使用Jquery从aspx页面调用这些方法.
我的要求是.
当我单击上载按钮时,它将开始从excel(BulkCopy)上传数据,并且它将在excel文件中包含数千个数据.
将数据从Excel文件传输到数据库时,我想在客户端浏览器上显示进度. 21500个中的1500个成功复制!在8/25/2011下午12:09
代码段: Web服务
Hi
I have create two functions in my WebService and i am calling those methods from aspx page using Jquery.
My requirement are.
When i click on upload button it will start uploading data from excel (BulkCopy), and it will contains thousands of data in excel file.
While transfering data from Excel file to Database i would like to display progress on client browser like. 1500 of 21500 copied successfully! at 8/25/2011 12:09 PM
Code snippet: Web Service
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string SaveFileData(string FileName, string ClientID, string FileAutoID)
{
try
{
DataTable dtExcelData = ExcelDataLoader(FileName, ClientID, "");
long TotalRecords = dtExcelData.Rows.Count;
long CopiedRecords = 0;
using (SqlConnection cn = new SqlConnection(GetConnectionString()))
{
SqlCommand cmdCopiedRecords = new SqlCommand("SELECT COUNT(*) FROM " + string.Format(DataBankTableFormat, FileAutoID) + ";", cn);
cn.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.BatchSize = BatchSize;
for (int colIndex = 0; colIndex < dtExcelData.Columns.Count; colIndex++)
{
copy.ColumnMappings.Add(colIndex, colIndex);
}
copy.DestinationTableName = string.Format(DataBankTableFormat, FileAutoID);
copy.WriteToServer(dtExcelData);
CopiedRecords = System.Convert.ToInt32(cmdCopiedRecords.ExecuteScalar());
HttpContext.Current.Cache["RecordStatus"] = string.Format("{0} of {1} copied successfully! at {2}", CopiedRecords, TotalRecords, DateTime.Now.ToString());
}
cn.Close();
}
Response = GetResponse(true, Convert.ToString(HttpContext.Current.Cache["RecordStatus"]));
}
catch (Exception ex)
{
Response = GetResponse(false, ex.Message);
}
return Response;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetRecordsInsertStatus(string FileAutoID)
{
try
{
Response = GetResponse(true, Convert.ToString(HttpContext.Current.Cache["RecordStatus"]));
}
catch (Exception ex)
{
Response = GetResponse(false, ex.Message);
}
return Response;
}
代码段: .aspx Jquery Ajax
Code snippet: .aspx Jquery Ajax
//------------------------------
// Save uploaded file data in database
//------------------------------
function SaveFileData() {
DisplayMessage("Uploading bulk data from file to database, this will take time please wait...", "Loading", false);
displayRecordStatusOn();
$.ajax({
type: "POST",
url: "SaveData.asmx/SaveFileData",
data: "{'FileName':'" + savedFileName + "', 'ClientID':'" + GetSelectedClient() + "','FileAutoID':'" + savedFileAutoID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
if (result.Success == "False") {
DisplayMessage("Error : " + result.Message, "Failed", false);
return;
}
else {
//DisplayMessage("Datatransfer process 100% completed", "Success", false);
displayRecordStatusOff();
DisplayMessage(result.Message, "Success", false);
alert("100% Done!!");
}
}
});
}
function displayRecordStatusOn() {
$.ajax({
type: "POST",
url: "SaveData.asmx/GetRecordsInsertStatus",
data: "{'FileAutoID':'" + savedFileAutoID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
if (result.Success == "False") {
DisplayMessage(result.Message, "Error", false);
}
else {
//I am not getting output here...
DisplayMessage(result.Message, "Information", false);
}
}
});
recordStatusTimer = setTimeout("displayRecordStatusOn()", 1500);
}
问题:-
我正在尝试使用ajax访问GetRecordsInsertStatus中HttpContext.Current.Cache ["RecordStatus"]的值,但它将始终返回空.
Problem:-
I am trying to access value of HttpContext.Current.Cache["RecordStatus"] in GetRecordsInsertStatus using ajax but it will always return empty.
推荐答案
.ajax({ 类型:" , 网址:" , 数据:" + savedFileName + " + GetSelectedClient()+ " ','FileAutoID':'" + savedFileAutoID + '} ", contentType:" , dataType:" , 成功:功能(响应){ var result = jQuery.parseJSON(response.d); 如果(结果.成功== " span>){ DisplayMessage(" + result.Message," 失败",返回; } 其他 { // DisplayMessage(数据传输过程已100%完成",成功",false); displayRecordStatusOff(); DisplayMessage(result.Message," , false ); alert(" ); } } }); } 函数 displayRecordStatusOn(){
.ajax({ type: "POST", url: "SaveData.asmx/SaveFileData", data: "{'FileName':'" + savedFileName + "', 'ClientID':'" + GetSelectedClient() + "','FileAutoID':'" + savedFileAutoID + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var result = jQuery.parseJSON(response.d); if (result.Success == "False") { DisplayMessage("Error : " + result.Message, "Failed", false); return; } else { //DisplayMessage("Datatransfer process 100% completed", "Success", false); displayRecordStatusOff(); DisplayMessage(result.Message, "Success", false); alert("100% Done!!"); } } }); } function displayRecordStatusOn() {
.ajax({ 类型:" , url:" , 数据:" + savedFileAutoID + " , contentType:" , dataType:" , 成功:功能(响应){ var result = jQuery.parseJSON(response.d); 如果(结果.成功== " span>){ DisplayMessage(result.Message," , false ); } 其他 { // 我在这里没有得到输出... DisplayMessage(result.Message," ,>错误); } } }); recordStatusTimer = setTimeout(" , 1500 ); }
.ajax({ type: "POST", url: "SaveData.asmx/GetRecordsInsertStatus", data: "{'FileAutoID':'" + savedFileAutoID + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var result = jQuery.parseJSON(response.d); if (result.Success == "False") { DisplayMessage(result.Message, "Error", false); } else { //I am not getting output here... DisplayMessage(result.Message, "Information", false); } } }); recordStatusTimer = setTimeout("displayRecordStatusOn()", 1500); }
问题:-
我正在尝试使用Ajax访问GetRecordsInsertStatus中HttpContext.Current.Cache ["RecordStatus"]的值,但它将始终返回空.
Problem:-
I am trying to access value of HttpContext.Current.Cache["RecordStatus"] in GetRecordsInsertStatus using ajax but it will always return empty.
尝试使用响应而不是结果.我很惊讶JavaScript没有抛出错误.
Try using response instead of result. I''m surprised JavaScript isn''t throwing an error.
DisplayMessage(result.Message, "Information", false);
在代码的那一点上,该函数不应该知道结果是什么.因为您将response指定为变量:
At that point in the code, that function shouldn''t know what result is. Because you specify response as the variable:
function (response)
我可能很容易在这方面犯错,但我认为可能就是这样!
祝你好运!
I could easily be wrong in this, but I think that might be it!
Good Luck!
这篇关于我如何在两个Ajax请求之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!