结束 AngularJS 连接

Ending an AngularJS connection

提问人:user199135 提问时间:11/23/2021 最后编辑:Kinglishuser199135 更新时间:11/23/2021 访问量:39

问:

我到底如何使用 AngularJS 做到这一点?

我正在使用此代码生成带有搜索栏的客户端表。ASP.NET MVC 是这里的服务器。

    app.controller('GenTable', ['$http' ,'$scope', '$window',
        function($http ,$scope, $window) 
        {
            console.log("I'm called");
            $scope.CallSearch = function()
            {
                if($scope.SearchField==null)
                {
                    $http.get("https://localhost:(portnumberhere)/project/all").then(function(response) 
                    {
                        $window.Project = response.data;
                        ProjectLength = Project.length;
                        console.log("I'm in get");
                    });

                }
                else
                {
                    PostData="";
                    PostData=$scope.SearchField;
                    $http.post("https://localhost:(portnumberhere)/project/filter",PostData).then(function(response) 
                    {
                        $window.Project = response.data;
                        ProjectLength = Project.length;
                        console.log("I'm in post with"+PostData);
                    });
                    PostData="";
                    console.log("I'm not in post with PostData as "+PostData);
                }
            }
            
            $http.get("https://localhost:(portnumberhere)/project/all").then(function(response) 
                {
                    $window.Project = response.data;
                    $scope.Project = $window.Project;
                    ProjectLength = Project.length;
                    console.log("I'm in get table");
                });
                console.log("I'm at end");
        }]);

它打印在我的控制台中

I'm called
I'm at end
I'm in get table

这会瘫痪任何其他请求,例如使用搜索栏。

angularjs http post get 连接

评论

0赞 Kinglish 11/23/2021
你的实际问题是什么?控制台输出基于您发布的代码是有意义的。你能详细说明一下你所期望的、对你来说没有发生的事情吗?
0赞 user199135 11/23/2021
该程序获取生成表所需的 sql 数据,但是当涉及到 if/else 语句(用于搜索栏)时,它无法对任何输入做出反应。
0赞 user199135 11/23/2021
即使我禁用了最后一个 http.get,我的 if/else 语句也不起作用。
0赞 Kinglish 11/23/2021
这里有几件事不对劲。首先,您的 if/else 语句位于 CallSearch() 函数中 - 是否正在调用该函数?你实际上是在$scope启动时发生的,而不是在实际函数内部。此外,如果您通过 发送 PostData,则该数据必须是一个对象,例如console.log('Im called')$http.postPostData={searchTerm: $scope.SearchField}
0赞 user199135 11/24/2021
无法确认或否认 PostData 是否应该是对象而不是字符串,因为现在我收到“原因:CORS 预检响应未成功”,尽管我将我的 WebApi.config 配置为具有此 <httpProtocol> <customHeaders> <add name=“Access-Control-Allow-Origin” value=“*” /> <add name=“Access-Control-Allow-Headers” value=“Content-Type” /> <add name=“Access-Control-Allow-Methods” value=“GET, POST、PUT、DELETE、OPTIONS“ /> </customHeaders> </httpProtocol>

答: 暂无答案