AngularJs $ http第一次无效,但在后续调用中

时间:2016-06-29 20:31:46

标签: angularjs spring

全部: 我在一个简单的HTML页面上有一个Iframe标签,而这个Iframe标签内部是一个HTML表单 - 我们称之为一个小部件。它是一种用户可以输入搜索条件并单击按钮以获取搜索结果的表单。我决定使用Angularjs对REST服务进行必要的DB调用,该服务将搜索结果返回给小部件进行显示。

在测试中,我在窗口小部件的文本框中输入一个值,该值是我知道在我测试的特定表中存在的值。

这是我的$ http get call:

            $http
        .get('http://localhost/XXXXService/folder/getfolders/' +
            $scope.formData.srchterm   **==>** mapped to search textbox

        ).then(function successCallback(response) {
            // this callback will be called asynchronously
            // when the response is available
            //$scope.formData = response;
            //$scope.nrdFolderDataArray = response;
            console.log('Success retrieving data.');
            console.log(response.length);
            console.log('response:');
            console.log(response);
        }, function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            console.log('Error occurred retrieving data.');
            console.log(response.status);
            console.log('returned data count:');
            console.log(response.size);
            console.log('response:');
            console.log(response);
        });

此调用位于名为" submitForm"的函数内。使用" ng-submit =" submitForm()"映射到窗口小部件提交按钮上的属性。是的,有几个日志语句,所以我可以尝试找出这里发生了什么。正在发生的事情是:在文本框中输入值后,单击搜索按钮的第一个时间.get命令不起作用。我没有调用REST服务。我知道这是因为我编写了REST服务,并确保在执行此操作时输入大量日志语句,并且REST服务的日志文件中没有日志语句。而是运行errorCallback块,我在控制台中看到所有这些日志语句。 "响应"第一次调用之后(对象?)看起来像这样:

Object {data: null, status: 0, config: Object, statusText: ""}  Method = GET Status = (canceled) type xhr

此外,在FF中,在第一次呼叫时,Firebug的Net选项卡中没有要查看的响应数据,但是在所有后续呼叫中都有要查看的响应数据。 所以,有趣的是,每次后续调用$ http.get调用都有效!我在REST服务方法的日志文件中看到日志语句,并且successCallback块运行,我可以通过那些console.log消息查看我的数据。

另外,如果我要更改文本框中的搜索值,那么我们将第一次搜索该新密钥的数据,再一次,该呼叫不起作用,但确实可以在后续工作点击"搜索"该新键值的按钮。

我真的不确定我在这里是否有CORS问题,因为除了第一次调用之外,每个后续的$ http.get调用都像冠军一样。它只是在搜索键之后的第一次调用或第一次调用已经改变了$ http.get不想工作。

我已尝试在.get调用中设置标头,我尝试过使用 @CrossOrigin(起源=" http://localhost/7001")按照 https://spring.io/guides/gs/rest-service-cors/但我在第一次调用此.get调用时仍然遇到此问题。

我现在已经处理了这个问题太多小时了,并且肯定会对为什么会发生这种情况有所了解。

我很抱歉这里很冗长。我想彻底解释我的问题。 请帮忙。 谢谢 -g

0 个答案:

没有答案