[英]Why does my back-end function loops, thus making my GET ajax call fail?
This was tested on Internet Explorer 10, but set in in a way that runs like IE8 这已在Internet Explorer 10上进行了测试,但设置方式类似于IE8
I have two ajax calls. 我有两个ajax电话。 The first one asynchronous and the second one is synchronous. 第一个是异步的,第二个是同步的。 In the first one, I get data from the days of a month. 在第一个中,我从一个月的几天中获取数据。 On success, it fires the synchronous ajax call in order to get the holidays. 成功时,它将触发同步ajax调用以获取假期。
$(function () {
/*some code*/
Load();
}
function Load() {
/* Some irrelevant code */
var today = new Date();
var selectedDate = (today.getMonth() + 1) + "/" + today.getFullYear();
//PopUp that appears when the Ajax calls are made.
var $waitDialog = $('<div id="waitingPopUp"></div>'); //I shortened the content of this variable.
$waitDialog.dialog({
zIndex: 3000,
title: 'Processing...',
dialogClass: "no-close",
resizable: false,
draggable: false,
modal: true,
closeOnEscape: false
});
ajaxGetMonthRecord(selectedDate);
}
function ajaxGetMonthRecord(selectedDate) {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: url + "/GetMonthData",
data: { 'monthRecord': selectedDate },
dataType: "json",
success: function (data) {
var holidays = ajaxGetHolidays(selectedDate);
createCalendar(data, selectedDate, holidays);
}
},
error: function (jqXhr, textStatus, errorThrown) {
alert("ERROR ON AJAX CALL N°1");
}
});
}
[Back-end function] (this works perfectly): [后端功能] (效果很好):
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
public List<MonthData> GetMonthData(string monthRecord)
{
var date = monthRecord.Split('/');
var list = (new MonthData()).GetAll(Convert.ToInt32(date[0]), Convert.ToInt32(date[1])).ToList();
return list;
}
And here is when it fails: 这是失败的时间:
function ajaxGetHolidays(selectedDate) {
var holidays;
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: url + "/GetHolidays",
data: { 'monthRecord': selectedDate },
dataType: "json",
async: false,
success: function (data) {
holidays = data;
},
error: function (jqXhr, textStatus, errorThrown) {
alert("ERROR ON AJAX CALL N°2");
}
});
return holidays;
}
[The Back-end function for the 2nd AJAX call ]: [第二个AJAX调用的后端功能 ]:
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
//This function starts running in a loop and never returns the List to the Ajax Call N°2
public List<Holiday> GetHolidays(string monthRecord)
{
var date = monthRecord.Split('/');
var list = (new Holiday()).GetAll(Convert.ToInt32(date[0]), Convert.ToInt32(date[1])).ToList();
return list;
}
When the second ajax call is set as async: false
, the back-end function fires and returns the data. 当第二个ajax调用设置为async: false
,后端函数将触发并返回数据。 But when it reaches the end of the back-end function, it's fired again, and again, on a continuous loop until the ajax call throws an error. 但是,当到达后端函数的末尾时,它将连续不断地触发,直到ajax调用引发错误为止。 This is the error information that returns the AJAX call. 这是返回AJAX调用的错误信息。
readyState: 0
textStatus: "error"
errorThrown:
ABORT_ERR 20
code 19
DATA_CLONE_ERR 25
DOMSTRING_SIZE_ERR 2
HIERARCHY_REQUEST_ERR 3
INDEX_SIZE_ERR 1
INUSE_ATTRIBUTE_ERR 10
INVALID_ACCESS_ERR 15
INVALID_CHARACTER_ERR 5
INVALID_MODIFICATION_ERR 13
INVALID_NODE_TYPE_ERR 24
INVALID_STATE_ERR 11
message "NetworkError"
name "NetworkError"
NAMESPACE_ERR 14
NETWORK_ERR 19
NO_DATA_ALLOWED_ERR 6
NO_MODIFICATION_ALLOWED_ERR 7
NOT_FOUND_ERR 8
NOT_SUPPORTED_ERR 9
PARSE_ERR 81
QUOTA_EXCEEDED_ERR 22
SECURITY_ERR 18
SERIALIZE_ERR 82
SYNTAX_ERR 12
TIMEOUT_ERR 23
TYPE_MISMATCH_ERR 17
URL_MISMATCH_ERR 21
VALIDATION_ERR 16
WRONG_DOCUMENT_ERR 4
But when I set async: true,
it either does the back-end function loop and throws an error with responseText:""
and errorThrown:""
, and also the console throws the following: 但是,当我设置async: true,
它要么执行后端功能循环并使用responseText:""
和errorThrown:""
引发错误,控制台也将引发以下错误:
XMLHttpRequest: Network Error 0x2f78, Could not complete the operation due to error 00002f78. XMLHttpRequest:网络错误0x2f78,由于错误00002f78而无法完成操作。
Or the back-end function is never fired and returns null on success (though this may be because the asynchronous call haven't finished yet) and the console doesn't catch anything funny. 否则,永远不会触发后端函数并在成功时返回null(尽管这可能是因为异步调用尚未完成),并且控制台没有捕获到任何有趣的东西。
I tried to set the troubling ajax call before the one that works, but the issue persists. 我试图在工作的Ajax调用之前设置麻烦的ajax调用,但是问题仍然存在。 What am I doing wrong and what can I do to fix this? 我做错了什么,该怎么办才能解决?
By the way, I found this on my google search, saying that two Ajax calls can't be made in IE because one aborts the other (from what I understood). 顺便说一句,我在Google搜索中发现了这一点 ,并说无法在IE中进行两次Ajax调用,因为一个调用中止了另一个调用(据我了解)。 Does anyone know about this issue? 有人知道这个问题吗?
Thanks in advance. 提前致谢。
try to use a different approach, using a callback. 尝试使用其他方法,使用回调。 something like this: 像这样的东西:
$(function () {
/*some code*/
Load();
});
function Load() {
var today = new Date();
var selectedDate = (today.getMonth() + 1) + "/" + today.getFullYear();
//PopUp that appears when the Ajax calls are made.
var $waitDialog = $('<div id="waitingPopUp"></div>'); //I shortened the
$waitDialog.dialog({
zIndex: 3000,
title: 'Processing...',
dialogClass: "no-close",
resizable: false,
draggable: false,
modal: true,
closeOnEscape: false
});
ajaxGetMonthRecord(selectedDate, function (result) {
var holidays = ajaxGetHolidays(selectedDate);
createCalendar(result, selectedDate, holidays);
});
}
function ajaxGetMonthRecord(selectedDate, callback) {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: url + "/GetMonthData",
data: { 'monthRecord': selectedDate },
dataType: "json",
success: function (data) {
if ($.isFunction(callback)) {
callback(data);
}
}
, error: function (jqXhr, textStatus, errorThrown) {
alert("ERROR ON AJAX CALL N°1");
}
});
}
After trying different approaches (thank you @Sandcar), I decided to attack it in a different way; 在尝试了不同的方法(感谢@Sandcar)之后,我决定以不同的方式进行攻击。 to let the first AJAX call finish before the second AJAX call starts. 在第二个AJAX通话开始之前让第一个AJAX通话结束。 This made the back-end function behave as expected (no loop made), but the AJAX call returned an error (the same one I posted in the question). 这使得后端函数的行为符合预期(不进行循环),但是AJAX调用返回了错误(我在问题中发帖的错误)。
Then it hit me that it could be something inside the Holiday class (I had an issue before, where I found out that setters needed to be declared, even if empty, in order for the AJAX call to work ). 然后让我感到震惊的是,这可能是在Holiday类中的事情(我之前遇到过一个问题,我发现需要声明设置器,即使它是空的也可以使AJAX调用起作用 )。
Turns out an attribute from the Holiday class (the one I was trying to get data from) was the issue: 问题出在Holiday类中(我试图从中获取数据的一种):
[DataMember]
public DateTime? Date
{
get
{
string date = string.Concat(this.Day, "/", this.Month, "/", this.Year);
return Convert.ToDateTime(date);
}
}
/*code for getters and setters for day, month, year and description about the holiday */
Apparently, AJAX doesn't like DateTime? 显然,AJAX不喜欢DateTime吗? types. 类型。 This may be resolved by parsing the Date to a string (If anyone thinks there's a proper way to resolve this, please feel free to reply and correct me). 这可以通过将Date解析为字符串来解决(如果有人认为有解决此问题的正确方法,请随时答复并纠正我)。 I ended up removing the [DataMember]
line , since that attribute is unnecesary for me (adding a setter didn't resolved the issue, BTW). 我最后删除了[DataMember]
行 ,因为该属性对我来说是不必要的(添加setter不能解决问题,顺便说一句)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.