简体   繁体   中英

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

I have two ajax calls. 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.

$(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 ]:

[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. 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. This is the error information that returns the AJAX call.

    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:

XMLHttpRequest: Network Error 0x2f78, Could not complete the operation due to error 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.

I tried to set the troubling ajax call before the one that works, but the issue persists. 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). 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; to let the first AJAX call finish before the second AJAX call starts. 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).

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 ).

Turns out an attribute from the Holiday class (the one I was trying to get data from) was the issue:

[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? 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). I ended up removing the [DataMember] line , since that attribute is unnecesary for me (adding a setter didn't resolved the issue, BTW).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM