0

当在我的页面中调用某些操作时,我对服务器上的两个不同方法进行了两次 ajax 调用(A、B)。大多数情况下,每个请求都会得到匹配的响应,但有时两个请求都会得到相同的响应!(其中一项请求 - A、A 或 B、B)

Ajax 调用是使用 JQuery 进行的,服务器方法是使用 Play 实现的!框架(在java中)。

有谁知道为什么会发生以及如何解决它?

谢谢!

阿贾克斯呼叫 A:

var renderTypePreviewPageRoute = jsRoutes.com.eyeview.connectad.controllers.solutions.FeedLibrary.getFeedTypePreviewPage(feedHashId, feedType);

    // Makes an ajax call that gets the rendered solution page
    $.ajax({

        // Sets the route (URL) of the server call
        url:renderTypePreviewPageRoute.url,

        // Sets the method (GET / POST) of the server call
        type:renderTypePreviewPageRoute.method,

        //data:{ hashId: feedHashId, feedType: feedType, withPreview: withPreview }-->

        // In case of success
        success:function(result) {

            var typePreviewElement = $('#typePreviewSection');

            // Set the feed preview section html content to the rendered content got from the server
            typePreviewElement.html(result);

            typePreviewElement.removeClass('hidden');

            $('#feedPreviewGrid tr:eq(1)').removeClass('hidden');

            if ($('#feedPreviewSection').is(':visible')){

                typePreviewElement.show('blind');
            }

            var feedURL = urlEle.val();
            if (waitForFileTypePreview && feedURL != "") {
                feedEditNS.renderFilePreviewSection(true);
            }
        },

        // In case of failure
        error:function(xhr, ajaxOptions, thrownError) {

            // Shows the error message
            showError(xhr.responseText);

            // Clears the preview section
            feedEditNS.clearTypePreviewSection();

            var feedURL = urlEle.val();
            if (waitForFileTypePreview && feedURL != "") {
                feedEditNS.renderFilePreviewSection(true);
            }
        }

Ajax 调用 B:

var renderFilePreviewPageRoute = jsRoutes.com.eyeview.connectad.controllers.solutions.FeedLibrary.getFeedFilePreviewPage(feedHashId);

    // Makes an ajax call that gets the rendered solution page
    $.ajax({

        // Sets the route (URL) of the server call
        url:renderFilePreviewPageRoute.url,

        // Sets the method (GET / POST) of the server call
        type:renderFilePreviewPageRoute.method,

        // In case of success
        success:function(result) {

            // Set the feed preview section html content to the rendered content got from the server
            $('#filePreviewSection').html(result);

            // Shows the feed preview section
            $('#verticalLine').show('blind');
            $('#leftShadow').show('blind');
            $('#rightShadow').show('blind');
            $('#feedPreviewSection').show('blind');

            feedEditNS.createDataTable(withHeaders);

            waitForFileTypePreview = false;
        },

        // In case of failure
        error:function(xhr, ajaxOptions, thrownError) {

            // Shows the error message
            showError(xhr.responseText);

            // Clears the preview section
            feedEditNS.clearFilePreviewSection();

            waitForFileTypePreview = false;
        }
4

2 回答 2

0

我无法解决问题。因此,我最终将两个调用组合到一个对单个服务器端方法的调用。此方法返回一个包含两个调用答案的 JSON 对象。

于 2012-08-22T07:30:30.310 回答
0

我遇到了这个确切的问题(3 年后......)我仍然不确定真正的问题是什么,但作为一种解决方法,我最终setTimeout()在我的 Angular 控制器中使用。

myApp.controller('myCtrl', function($scope, myRestApi) {

    $scope.restCallOne = function() {
        myRestApi.callOne().then(
            // handle result one
        );
    };

    $scope.restCallTwo = function() {
        myRestApi.callTwo().then(
            // handle result two
        );
    };

    // loads each time the view is shown
    // *** race condition when calling consecutively without a delay ***
    //$scope.restCallOne();
    setTimeout($scope.restCallOne, 100);
    $scope.restCallTwo();

});
于 2015-05-19T04:34:00.190 回答