0

I have very difficult problem. I have a page where a user fills some data and click on button which save all parameter into a json object. After saving data locally user click another button which invoke ajax method which internally called a web service method, added in the same website, to store the data on server.

My problem is this, when a user save the data on locally and leave the system and coming back after sometime, it click the second button to store data on server. The ajax method throws error(sometime 401 and sometime net::ERR_CONNECTION_RESET). but when he click that button again everything run successfully.

I have asked my senior and he now wants that it logout on this situation. But I am confused that how can i validate server side session on client side in order to logout user.

The ajax method which called by the click of second button

// symbol "$" is used to make attribute property in XML. for this symbol "$" is replaced by symbol "@" in aspx.cs file.
    //this method is used to call Ajax Helper Class method.
    function MethodWithComplexParameterClient(jsEmp) {
        try {
            var jsonText = JSON.stringify(jsEmp); //Convert javascript value to a JSON String
            var rowactioncount = "0";
            if ($("#ContentPlaceHolder1_RuleGeneratorRBL input")[1].checked) {
                rowactioncount = $("#ContentPlaceHolder1_RowActionCountHdn").val();
            }

            $.ajax(
                                        {
                                            type: "POST",
                                            url: "AjaxHelperWebService.asmx/MethodWithComplexParameter",
                                            data: '{"jsonParam": ' + jsonText + ',"rowactioncount":"' + rowactioncount + '"}',
                                            contentType: "application/json; charset=utf-8",
                                            dataType: "json",
                                            success: function (msg) {
                                                if (Number(msg.d) == 1) {
                                                    if ($("#ContentPlaceHolder1_RuleGeneratorRBL input")[1].checked) {
                                                        alert("Rule Uploaded Successfuly. Rule Name: " + '"' + $("#ContentPlaceHolder1_RuleNameTB").val().trim() + '.xml"');
                                                        var IsEMIProgramtype = false;
                                                        for (var i = 0; i < $("#RuleFileUL").children().length; i++) {
                                                            if (($("#RuleFileUL").children()[i].id).contains('EMI')) {
                                                                IsEMIProgramtype = true;
                                                                break;
                                                            }
                                                        }
                                                        if (IsEMIProgramtype)
                                                            alert("Scheme has been edited. Please update HDFC internal TID, RBL internal TID, SBI SKU Code if mapped with old scheme");

                                                    }
                                                    else {
                                                        var lRuleName = $("#ContentPlaceHolder1_RuleNameTB").val().trim();
                                                        var ruleNameLength = lRuleName.length;
                                                        var next_index = 50;
                                                        var madeOutRuleName = "";
                                                        for (ii = 0; ii < ruleNameLength; ii = ii + 50) {
                                                            var lSplitedRuleName = lRuleName.substring(ii, next_index);
                                                            madeOutRuleName = madeOutRuleName + lSplitedRuleName + "\n";
                                                            next_index = next_index + 50;
                                                        }

                                                        if (next_index == 50) {
                                                            alert("Rule Created Successfuly. Rule Name: " + '"' + lRuleName + '.xml"');
                                                        }
                                                        else {
                                                            alert("Rule Created Successfuly. Rule Name: " + '"' + madeOutRuleName + '.xml"');
                                                        }
                                                    }


                                                    $("#RuleFileUL").empty();
                                                    $("#ContentPlaceHolder1_RuleFileLblTB").val("Rule");
                                                    document.getElementById('ContentPlaceHolder1_RuleFileLblTB').style.height = "15px";
                                                    document.getElementById('ContentPlaceHolder1_ParamHeaderLbl').style.display = 'none';
                                                    document.getElementById('ContentPlaceHolder1_DefaultParamTable').style.display = 'none';
                                                    document.getElementById("ContentPlaceHolder1_RulePropertyTable").style.display = "none";
                                                    document.getElementById("ContentPlaceHolder1_ValocityCheckTbl").style.display = "none";
                                                    $("#ContentPlaceHolder1_ExistingSchemaSuggestTB").val("");
                                                    document.getElementById("ContentPlaceHolder1_ExistingSchemaTbl").style.display = "none";
                                                    document.getElementById("ContentPlaceHolder1_SchemaPropertiesSaveBtnTbl").style.display = "none";

                                                    $("#ContentPlaceHolder1_RuleNameTB").val("");
                                                    $("#RuleStartHrDTPickerTB").val("");
                                                    $("#RuleEndHrDTPickerTB").val("");

                                                    Object.keys(ProgramTypeObjHT).forEach(function (key) {
                                                        delete ProgramTypeObjHT[key];
                                                    });
                                                    Object.keys(SchemaObjHT).forEach(function (key) {
                                                        delete SchemaObjHT[key];
                                                    });
                                                    Object.keys(EMIObjHT).forEach(function (key) {
                                                        delete EMIObjHT[key];
                                                    });
                                                    Object.keys(DefaultPTParams).forEach(function (key) {
                                                        delete DefaultPTParams[key];
                                                    });
                                                    Object.keys(SchemaParams).forEach(function (key) {
                                                        delete SchemaParams[key];
                                                    });
                                                    Object.keys(EMIParamsHT).forEach(function (key) {
                                                        delete EMIParamsHT[key];
                                                    });
                                                }
                                                else if (msg.d == "2") {
                                                    alert("Rule Generation Failed.");
                                                }
                                                else if (msg.d == "4") {
                                                    $("#ContentPlaceHolder1_RuleNameTB").val("");
                                                    alert("Rule Name Already Exists, Please Enter Different Rule Name.");
                                                    $("#ContentPlaceHolder1_RuleNameTB").focus();
                                                }
                                                else if (msg.d == "5") {
                                                    alert("Duplicate Schema Name Found. Please Check Schema Names.");
                                                }
                                                else if (msg.d == "6") {
                                                    alert("Rule Is Updated By Someone Else. Please Refresh.");
                                                }
                                                else {
                                                    alert("Exception Occurred.");
                                                }
                                            },
                                            error: function ( x, e) {
                                                debugger;
                                                if (x.status == 401) {
                                                    alert("The call to the server side failed. due to unauthorised access. " + x.status);
                                                    CreateXMLBtnClientClick();
                                                }
                                                else {
                                                    alert("The call to the server side failed. " + x.responseText);
                                                    console.log(x, e);
                                            console.log(x.responseText);
                                                    console.log(x.status);
                                                    console.log(e);
            [![enter image description here][1]][1]                                 }
                                            }
                                        }
                                    );
            return false;
        }
        catch (err) {
            alert(err);
        }
    }

1 Answers1

1

The ajax method throws error

Well, catch this error and if it happens redirect to the logout page. Pretty simple, and when you catch the AJAX error you can do:

window.location.href = 'logout.html' or whatever. See here: Window.location.href and Window.open () methods in JavaScript

You did not post enough information for more specific help.

Community
  • 1
  • 1
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • Thanks for your quick response, but it gives different error in different time. Sometime it say unauthorized access which i have tried to capture but some time it throws error that connection has been reset. – Mohd Ismail Siddiqui Feb 01 '16 at 13:49
  • You posted your code, now post what results you are getting when you submit the data. You have a bunch of alerts, but you do not say which ones you see when you see the errors. This is an impossible to answer question if you do not give better information. – mikeb Feb 01 '16 at 15:27
  • 1
    actually i am talking about error:function (x,e) where sometimes it gives error x.status = 401 and sometime it gives error "" but the console log show me err_connection_reset error. – Mohd Ismail Siddiqui Feb 02 '16 at 04:48