在从按钮单击调用的ajx方法调用webservice方法之前验证会话

时间:2016-02-01 13:31:44

标签: javascript c# jquery asp.net ajax

我有很难的问题。我有一个用户填充一些数据的页面,然后单击按钮将所有参数保存到json对象中。在本地保存数据后,用户单击另一个按钮,该按钮调用ajax方法,该方法在内部称为Web服务方法,添加在同一网站中,以将数据存储在服务器上。

我的问题是,当用户在本地保存数据并离开系统并在一段时间后返回时,它会单击第二个按钮将数据存储在服务器上。 ajax方法抛出错误(有时401和某些时候net :: ERR_CONNECTION_RESET)。但当他再次点击该按钮时,一切都成功运行。

我问过我的大四学生他现在想要退出这种情况。但我很困惑,我如何在客户端验证服务器端会话以退出用户。

通过点击第二个按钮调用的ajax方法

// 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 个答案:

答案 0 :(得分:1)

\s

好吧,抓住这个错误,如果它发生重定向到注销页面。非常简单,当你发现AJAX错误时,你可以这样做:

The ajax method throws error或其他什么。见这里:Window.location.href and Window.open () methods in JavaScript

您没有发布足够的信息以获得更具体的帮助。