2

以下のように、すべての Ajax 呼び出しに共通の機能を記述しようとしています。下のように分けて書くとアボートがうまくいきませんが、同じ ajax に beforesend を追加するとうまくいきます。

 $(document).ready(function () {

                        $.ajax({
                            beforeSend: function (jqXHR, settings) {
                                check = TestCallMethod();
                                if (check.responseText != "true") {
                                    alert("Call Failed");
                                    jqXHR.abort(event);
                                }
                            }
                        });

            $.ajax({
//                beforeSend: function (jqXHR, settings) {
//                    debugger;
//                    check = TestCallMethod();
//                    if (check.responseText != "true") {
//                        alert("Call Failed");
//                        jqXHR.abort();
//                    }
//                },
                cache: false,
                url: '@Url.Action("TestCall2", "Home")',
                success: function (data) {
                    alert(data);
                },
                error: function (request, status, error) {
                    alert(request.responseText);
                }
            });
        });

        function TestCallMethod()
        {
            return  $.ajax({
                url: '@Url.Action("TestCall", "Home")',
                async: false,
                cache: false,
                success: function (data) {
                    return data;
                },
                error: function (request, status, error) {
                    alert(request.responseText);
                    return false;
                }

            });
        } 

一般的なajax beforsendの中止を達成するには?

4

1 に答える 1

1

Ajax のグローバルに共通の設定を適用できます。お役に立てば幸いです。

$.ajaxSetup({
    global: true,
    beforeSend: function (jqXHR, settings) {
        check = TestCallMethod();
            if (check.responseText != "true") {
                alert("Call Failed");
                jqXHR.abort(event);
            }
    }
});
于 2013-12-09T13:54:16.533 に答える