【问题标题】:jQuery Ajax Callback does not get ContextjQuery Ajax 回调没有获取上下文
【发布时间】:2012-11-20 04:04:44
【问题描述】:

我有一个 HTML 页面,如 http://jsfiddle.net/Lijo/zPAfF/6/ 所示。当“searchReportButton”按钮被点击时,一个javascript函数“checkSession”被调用。在“checkSession”函数中,有一个jquery.ajax 对下面列出的web method 的调用。 Web 方法工作正常,我可以在 javascript 成功回调中将结果警告为“ACTIVE”。

我已将上下文设置为这个。

 context: this,  //CONTEXT 

我希望下面的代码能够提醒“正在加载...”文本。假设this 将在success callback 中工作,因为设置了context。但它没有显示文字。

if (result == "ACTIVE") 
{       
    var selectedTable = $(this).closest('.dateFilter').siblings('.sentDatesSection').find('.reportSentDatesDiv');

    alert($(selectedTable).text());

}

问题

为了在回调中获取上下文需要更正什么?

网络方法

    [WebMethod(CacheDuration = 0)]
    public static string CheckSessionExpiry(string empID)
    {

        return "ACTIVE";
    }

jQuery

$(document).ready(function() {
    var searchReportButton = $('.searchReportButton');
    searchReportButton.click(function() {
        var selectedTable = $(this).closest('.dateFilter').siblings('.sentDatesSection').find('.reportSentDatesDiv');
        alert($(selectedTable).text());
        checkSession();
        return false;
    });
});

function checkSession() {
    var empID = 101;
    alert('Function Reached');
    $.ajax({
        type: "POST",
        url: "Error.aspx/CheckSessionExpiry",
        data: '{"empID": "' + empID + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        context: this,
        //CONTEXT
        success: handleSessionResult
    });
}

function handleSessionResult(result) {
    if (result.hasOwnProperty("d")) {
        result = result.d
    }
    if (result == "ACTIVE") {
        var selectedTable = $(this).closest('.dateFilter').siblings('.sentDatesSection').find('.reportSentDatesDiv');
        alert($(selectedTable).text());
    }
}​

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Report List </title>

</head>
<body>
<form>

<div id="wrapper">
    <div id="container">
        <div id="centralContainer">
            <div id="mainContainer">
                <div id="contentHolderDiv" class="contentHolderDiv">
                    <div id="searchContainer" class="searchContainer">
                        <div id="entryValues" class="repeaterContainer">
                            <div class="repeaterTableBorder">
                                <div class="repeaterRecord">
                                    <div class="repeaterIdentifier">
                                        <div class="reportTitle">
                                            Daily Financial Transaction:
                                        </div>
                                    </div>

                                    <div class="viewLogTitle">
                                        <div class="viewLogText">
                                            View Report Log
                                        </div>
                                    </div>
                                    <div id="reportLog" class="reportLog">
                                        <div class="dateFilter">
                                            <div class="filterElements">
                                                <div class="filterContents">
                                                    <input type="submit" name="ctl00$detailContentPlaceholder$repReports$ctl00$btnSubmit"
                                                        value="Get Reports" id="detailContentPlaceholder_repReports_btnSubmit_0" class="searchReportButton" />
                                                </div>
                                            </div>
                                        </div>
                                        <div class="sentDatesSection">
                                            <div class="reportSentDatesDiv">
                                                <p>
                                                    Loading...</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="clear">
                    </div>
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
    </div>
</div>
</form>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    this 作为参数传递给checkSession 函数:

    checkSession(this);
    

    然后:

    function checkSession(context) {
        var empID = 101;
        alert('Function Reached');
        $.ajax({
            type: "POST",
            url: "Error.aspx/CheckSessionExpiry",
            data: JSON.stringify({ empID: empID }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            context: context,  //CONTEXT
            success: handleSessionResult
    
            }
        });
    }
    

    还要注意我如何修复您的 $.ajax 调用以使用 JSON.stringify 方法,以确保请求是正确的 JSON 格式。 从不,我再说一遍,绝对从不使用字符串连接(+ 运算符)来构建 JSON。 JSON.stringify 方法原生内置于所有现代浏览器中。如果您必须支持来自 Mesozoic era 的某些浏览器,例如 IE6 或其他浏览器,您可以将 json2.js 脚本引用到您的页面。

    【讨论】:

      【解决方案2】:

      或者,您可以使用call

      checkSession.call(this);
      

      或者,它的兄弟apply

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-08
        • 1970-01-01
        • 2012-11-05
        • 1970-01-01
        • 1970-01-01
        • 2015-02-13
        • 2011-11-08
        • 2013-02-05
        相关资源
        最近更新 更多